Simulate traffic density with labels from CSV file by the speed of vehicles in Jupyter Notebook using Tkinter

Erdem YAZAN
4 min readNov 23, 2018

This note has been prepared for those who have previously designed the GUI in Python and for those with experience in Python programming language.

The main logic of the program is the window that symbolizes 4 segments in the CSV file. With the Change button, you can observe the changes in the label according to the values in the file.

Illustration of sample road-I
Illustration of sample road-II

Here we do not take into account the shape of the road we received the information. The goal is to be close to real-life roads.

Sample speed values for each segment

We have speeds for each segment in our CSV file. (AB-BC-CD-DE)

Coloring requirements

We have speed conditions to color the roads. Looking at these colors, we make further estimates of the traffic density on the road. In this way, we can observe the color changes until the end of the CSV file.

After understanding the basic logic of the program, we can look at its codes.

The Tkinter module (“Tk interface”) is the standard Python interface to the Tk GUI toolkit. It is a thin object-oriented layer on top of Tcl/Tk. Tkinter is not the only GUI programming toolkit for Python. It is, however, the most commonly used one. All you need to do is to import one module;

Or, more often:

Our GetColour function allows us to color according to the speeds in the CSV file.

In our DarkenLabel function, we read the CSV file.

And we put the column names inside the CSV file into variables (Velocity_D, Run, Segment).

We’ve defined our maxcount variable to print speeds as much as the run column in our CSV file.

The cnt variable is used to color and print on the screen what number of segments we have.

Creating an instance of Tk initializes this interpreter and creates the root window. In short, Tk() allows us to create an interface. We have assigned our labels red by default.

We create our labels and button with grid structure. We have created our labels and buttons in the window as a row-column, in order to achieve a more accurate result.

The first screen of the program(Default view)

The digit 1 in the button corresponds to the run column in the CSV file.

Respectively; Atlantic-St.John’s, St.Johns-Beach Blc, Beach Blv.-Town Center Pkwy, Town Center-J Turner Butler Blv. corresponds to Segment-A Segment-B, Segment-C, and Segment-D. So the city names. (City names in CSV file)

You can observe the color change according to speed.

Everything is ready :)

Hopefully, it benefits you..

--

--