Filed in Python | Tkinter Leave a comment

While I was doing the profiling it became clear that a large part of the processing is devoted to just displaying images on the screen. Of course the graphical display is useful but there are times where I want to leave the program running for long periods of time and won’t be looking at it at all. So for that reason I decided to learn how to turn the display on and off. The screen capture below shows the current state of the graphical display.


As you can see I’ve added three buttons (without a great deal of aesthetic┬ácare) with one of the buttons giving the option to turn the display on or off. I’ll add some profiling numbers but with the display off the update rate is about 3 times faster than when the display is on. Alternately you can get the same update rate for a third of the processor load. Adding the option to disable the display required another architectural change to the code much like changing to the Tkinter graphical display. However, doing so made it very easy to add other options that I’ll talk about later.

The standard way of making the status of variables available to all Python modules is to declare them in a configuration file that gets imported into the modules that use those variables. I took the variables I’ve defined (number of rows, columns, update rate, etc.) and moved them into a separate file, gol_config.py that I then import as config.

The first step is to create the button in the main module gol.py:

 #display on/off button
 bttn_display = tk.Button(root, text="Display On/Off", command=gol_defs.display_state)

This creates the button and defines the function, gol_defs.display_state, called when the button is pressed. This function is very simple and just changes the state of the Boolean variable in gol_configs.py

 def display_state():
     #change state of display to turn it on or off
     config.display = not(config.display)

Then in the main program loop the value of config.display is checked and used to control whether the display is updated or not.

if config.display == True:
    #create state image
    pgm = gol_defs.write_file(currentState)
    state = tk.PhotoImage(data=pgm)
    #set image size
    state = state.zoom(width, height)
    tkimg[0] = state
    #update label with image
    label.configure(image = tkimg[0])