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.
Open a new file in your text editor and enter the following two lines of code at the top:
!/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.
Enter the following two lines:
c = Canvas() c.pack()
These create an instance of the Canvas widget and prepare it for display.
Create an array of vertices 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.
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.
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 colour and a red border. The final line of code launches the application. It continues to run until the user closes the window.
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
On Windows this is unnecessary. To see the star, run the script from the command line by typing:
- "Python in a Nutshell"; Alex Martelli; 2003
- "Programming Python, 3rd Edition"; Mark Lutz; 2006
- Use only a plain text or code editor. Word processors add text formatting that will prevent the Python interpreter from reading the script.