Graphical User Interface in Python:
A Traffic Light Simulation
Yasiris Ortiz |12/2/2019
INTRODUCTION
This is mainly for new users in Python. Graphical User Interface (GUI) is a form of user interface that allows users to interact with items such as windows and icons. Python provides multiple GUI options. Here, a simulation of a traffic light shows how to use GUI to simulate traffic directions. The outcome of this program will be a traffic light that turns on and lasts 10 seconds every time the user clicks a bottom.
.
Installation
First, install Python and import three different libraries that will be used for the program. For access download the software at https://www.python.org/. Fig.1 shows both steps. Once Python is installed, open the text editor to write the code and import the libraries to run the program. Import tkinter, the standard library that provides an object-oriented interface to the Tk GUI toolkit. Import time is used to set the time for the lights and import asyncio is a concurrency module for coroutines used to turn on/off the lights.
Draw a window
Design a window by creating a variable and using the Tk () method as shown in Fig.2. In this case, the variable is called root. Give the arguments by calling the method’s title, configure, and geometry. Use Canvas, a rectangular area to place graphics, text, and widgets. The values given can be changed and will not affect the output of the program.
Background
Select the image you would to like to have as background for the traffic light. Make sure it has the right size to fit in the canvas made above. Fig.3 shows the image selected for this program. It is imported by naming the file name of the image. PhtotoImage is the method used to return an image object.
Create Circles
Circles are created by using create_oval method and adding the dimensions for the size. As shown in Fig.4, I created three variables adding these arguments plus specifying the color of the lights, gray. The circles are placed on the image we imported before. All lights are gray-colored which means they are deactivated or turned off. A small function is created for this step, def deactivate(light).
ButtonClicked Functions
Define three functions for each button and each light color as shown in Fig. 6. it has the configuration of tuning on for 10 seconds and then turning off. Fig.5 shows the colors given to each circle from the functions created. Go, STOP and SLOW are the bottoms that interact with the user. See Fig.7. Lastly, closet the program by typing root.mainloop() in your program and run.
Follow the above steps to create your own traffic lights. Python allows users to create many programs using Graphical User Interface. Compared to other programming languages Python is a simpler and more readable language perfect for beginners.
References
Download Python. (n.d.). Retrieved December 2, 2019, from https://www.python.org/downloads/.
Traffic light vector image on VectorStock. (n.d.). Retrieved December 2, 2019, from https://www.vectorstock.com/royalty-free-vector/traffic-light-vector-493405.