How to Draw a Five Point Star Using Python Language

Techwalla may earn compensation through affiliate links in this story. Learn more about our affiliate and product review process here.
A five-pointed star

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.

Advertisement

Step 1

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

Video of the Day

#!/usr/bin/python from Tkinter import Canvas

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.

Advertisement

Step 2

Enter the following two lines:

c = Canvas() c.pack()

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

Advertisement

Step 3

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

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

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.

Advertisement

Advertisement

Step 4

Enter the following two lines:

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

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.

Advertisement

Step 5

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

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

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 color and a red border. The final line of code launches the application. It continues to run until the user closes the window.

Advertisement

Step 6

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 :

chmod +x star.py

Advertisement

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

./star.py

Video of the Day

Advertisement

Advertisement

Report an Issue

screenshot of the current page

Screenshot loading...