We Value Your Privacy

We and our partners use technology such as cookies on our site to personalise content and ads, provide social media features, and analyse our traffic. Click below to consent to the use of this technology across the web. You can change your mind and change your consent choices at anytime by returning to this site.

Update Consent
Loading ...

How to Draw a Five Point Star Using Python Language

Updated July 20, 2017

If you use a good graphics library, writing Python programming language that draws five-pointed stars, or any other simple two-dimensional geometric shapes, can be quick and easy. Tkinter is the most widely used cross-platform GUI (graphical user interface) library, and, not coincidentally, comes bundled with most Python distributions. Chances are that if you have a standard Python environment on your computer, you already have this library installed. It has all of the standard GUI widgets needed to create robust, interactive user interfaces. For drawing two-dimensional shapes, such as stars, it includes the easy to use Canvas widget.

Loading ...
  1. Open a new file in your text editor and enter the following two lines of code at the top:

  2. !/usr/bin/python from Tkinter import Canvas

  3. When running on any Unix/Linux/OS X variant, the first line will tell the operating system shell where to locate the Python interpreter; a Windows command shell will ignore the first line. The next line imports the necessary Tkinter widget class, Canvas, for use in subsequent code.

  4. Enter the following two lines:

  5. c = Canvas() c.pack()

  6. These create an instance of the Canvas widget and prepare it for display.

  7. Create an array of vertices for the five-pointed star shape by entering this line of code:

  8. verts = [10,40,40,40,50,10,60,40,90,40,65,60,75,90,50,70,25,90,35,60]

  9. Every two numbers in this array represent the x and y coordinates of a single vertex of the star. Ten vertexes make a five-pointed star; thus, there are 20 elements in the array. In computer graphics programming lingo, this array has a "stride" of two, that is, every two elements contain all of the information necessary to display one vertex.

  10. Enter the following two lines:

  11. for i in range(len(verts)): verts[i] += 100

  12. While not necessary to display the star shape in a window, this loop demonstrates a simple method of positioning the shape. In the terminology of computer graphics programming, this is called a "translation." The vertex array defines the original position of the shape; this translation moves the shape along the positive x and y axes 100 pixels. You can increase or decrease this diagonal translation by changing the value to something other than 100.

  13. Add these last two lines and save the file as "star.py":

  14. c.create_polygon(verts, fill='orange', outline='red') c.mainloop()

  15. As the function name implies, this code creates the star-shaped polygon using the vertex positions in the array. By default the canvas draws the star black, but this code overrides the default with an orange fill colour and a red border. The final line of code launches the application. It continues to run until the user closes the window.

  16. Open a shell, navigate to the directory containing the new code file and execute the following command at the command line if you're on a Unix/Linux/OS X system :

  17. chmod +x star.py

  18. On Windows this is unnecessary. To see the star, run the script from the command line by typing:

  19. ./star.py

  20. Tip

    Use only a plain text or code editor. Word processors add text formatting that will prevent the Python interpreter from reading the script.

Loading ...

Things You'll Need

  • Python scripting environment
  • Tkinter GUI library
  • Plain text or code editor

About the Author

Simone Spada holds a Bachelor of Arts in creative writing from UC Santa Cruz and a Master of Science in photography from Brooks Institute of Photography. He has written technical and instructional materials while working in the 3-D computer graphics industry as well as local business news for Santa Barbara’s Noozhawk.com.

Loading ...