Tkinter GUI start button registering input but not restarting program

Question:

Brief explanation of my program (or what it’s meant to do):

I have created a simulation program that models amoeba populations in Pygame. The program uses two classes – Main and Amoeba. The Main class runs the simulation and displays the results on a Pygame window and a Matplotlib plot. The Amoeba class models the properties and behavior of each amoeba in the population, including its maturing speed, age, speed, and movement direction. The simulation runs in a loop until the "q" key is pressed or the simulation is stopped. The GUI is created using the Tkinter library, which allows the user to interact with the simulation by starting and stopping it. The simulation updates the amoeba population and displays their movements on the Pygame window and updates the Matplotlib plot every 100 steps. The plot displays the average maturing speed and the reproduction rate of the amoeba population.

My issue is that whilst the stop button in the GUI works fine, the start button does not. It registers being pressed and actually outputs the variable it is meant to change to the terminal (the running variable which you can see more of in the code). So the issue is not in the button itself, but rather the way in which the program is restarted. I have tried to do this via if statements and run flags but it has failed. There are no error messages, the program just remains paused.

Here is the code to run the simulation from my Main.py file (other initialisation code before this):

def run_simulation():
    global step_counter
    global num_collisions
    global run_flag
    while run_flag:

        if globalvars.running:
            #main code here
            
        else:
            run_flag = False


gc.root = tk.Tk()
app = gc.GUI(gc.root)
app.root.after(100, run_simulation)
gc.root.mainloop()

This is the code from my GUI class:

import tkinter as tk
import globalvars

class GUI:
    def __init__(self,root):
        self.root = root
        self.root.title("Graphical User Interface")
        self.root.geometry("200x200")
        self.startbutton = tk.Button(root, bg="green", text="Start", command=self.start)
        self.startbutton.pack()
        self.stopbutton = tk.Button(root, bg="red", text="Stop", command=self.stop)
        self.stopbutton.pack()
    
    def start(self):
        globalvars.running = True
        print(globalvars.running)
    
    def stop(self):
        globalvars.running = False
        print(globalvars.running)

Also in a globalvars.py file I store global variables which includes the running var.

Would you mind explaining the issue please?

Asked By: AtomProgrammer

||

Answers:

There’s a logic error in the application: when stop() is called it sets globalvars.running = False. This means, in run_simulation() the else branch is executed which turns run_flag = False.

This variable is never reset to True!

So the while loop is left and never entered again and #main code here not executed.

In addition to setting run_flag = True, function run_simulation() needs to be called from start().

Turned my earlier comment into an answer so it can be accepted and the question resolved.

Answered By: Friedrich
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.