Tkinter 图形用户界面启动按钮注册输入但不重新启动程序

huangapple go评论59阅读模式
英文:

Tkinter GUI start button registering input but not restarting program

问题

简要解释我的程序(或者说它的目的):

我创建了一个模拟程序,用Pygame模拟阿米巴群体。该程序使用两个类 - Main和Amoeba。Main类运行模拟并在Pygame窗口和Matplotlib图上显示结果。Amoeba类模拟群体中每个阿米巴的属性和行为,包括其成熟速度、年龄、速度和移动方向。模拟在循环中运行,直到按下“q”键或停止模拟。使用Tkinter库创建GUI,允许用户通过启动和停止与模拟的交互。模拟更新阿米巴群体并在Pygame窗口上显示它们的移动,并在每100步更新Matplotlib图。该图显示阿米巴群体的平均成熟速度和繁殖率。

我的问题是,虽然GUI中的停止按钮工作正常,但启动按钮却不起作用。它注册为被按下,并且实际上将其应该改变的变量输出到终端(你可以在代码中看到的running变量)。因此,问题不在于按钮本身,而是程序重新启动的方式。我尝试过使用if语句和运行标志来实现这一点,但是失败了。没有错误消息,程序仅保持暂停状态。

以下是从我的Main.py文件运行模拟的代码(此代码之前有其他初始化代码):

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

        if globalvars.running:
            #主要代码在这里
            
        else:
            run_flag = False


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

这是我的GUI类中的代码:

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)

还有一个globalvars.py文件,其中存储了包括running变量在内的全局变量。

请问你能解释一下问题吗?

英文:

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?

答案1

得分: 1

应用程序中存在逻辑错误:当调用stop()时,它将globalvars.running设置为False。这意味着在run_simulation()中执行else分支,将run_flag设置为False

这个变量永远不会被重置为True!

因此,while循环被跳出,永远不会再次进入,#main code here也不会被执行。

除了设置run_flag = True之外,还需要从start()调用run_simulation()函数。

将我之前的评论转化为答案,以便可以被接受并解决问题。

英文:

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.

huangapple
  • 本文由 发表于 2023年2月8日 23:43:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75388233.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定