【问题标题】:Tkinter won't run loop in backTkinter 不会在后面运行循环
【发布时间】:2021-05-12 07:55:30
【问题描述】:

我有这个程序,当你按下是或否时输出一些东西,但它不想开始我的循环,直到我关闭 tkinter 窗口。 tkinter 窗口的目标是在我按下 no 时停止循环,停止按钮并不是真正必要的,我只是把它放在那里关闭窗口。

代码: 将 tkinter 导入为 tk

choice = False

def ja():
    global choice
    choice == True
    print("1 works")    # To check if this works

def nee():
    global choice
    choice == False
    print("2 works")    # Also to check if works

def prog():
    while True:
        if choice:
            print("3 works")
            # Program here

        if not choice:
            print("4 works")
            # Program here
            
root = tk.Tk()
root.title("Bot")
label1 = tk.Label(root, text="continue?", width=75, height=25)
Yes = tk.Button(root, text="Yes", width=25, height=5, command=ja)
No = tk.Button(root, text="No", width=25, height=5, command=nee)
Stop= tk.Button(root, text="Stop", width=25, height=5, command=root.quit)

label1.pack()
Yes.pack(fill=tk.Y, side=tk.LEFT)
No.pack(fill=tk.Y, side=tk.LEFT)
Stop.pack(fill=tk.Y, side=tk.LEFT)

root.mainloop()
prog()

当我按下是和否 2 次然后停止时的结果(注意:当你按下停止时,循环只会播放直到 ide 崩溃。我知道这一点以及我的项目需要它,所以这不是错误。):

1 works
1 works
2 works
2 works
2 works
4 works
4 works
4 works
4 works
....

【问题讨论】:

    标签: python loops tkinter


    【解决方案1】:

    使用afterprog() 函数在 mainloop() 之后调用,所以一旦你关闭窗口,while 循环就会开始。

    import tkinter as tk
    global choice
    choice=False
    def ja():
        
        choice == True
        print("1 works")    # To check if this works
    
    def nee():
        
        choice == False
        print("2 works")    # Also to check if works
    
    def prog():
        
        if choice:
            print("3 works")
            # Program here
        elif not choice:
            print("4 works")
            # Program here
        root.after(20,prog)
                
    root = tk.Tk()
    root.title("Bot")
    label1 = tk.Label(root, text="continue?", width=75, height=25)
    Yes = tk.Button(root, text="Yes", width=25, height=5, command=ja)
    No = tk.Button(root, text="No", width=25, height=5, command=nee)
    Stop= tk.Button(root, text="Stop", width=25, height=5, command=lambda: root.destroy())
    
    label1.pack()
    Yes.pack(fill=tk.Y, side=tk.LEFT)
    No.pack(fill=tk.Y, side=tk.LEFT)
    Stop.pack(fill=tk.Y, side=tk.LEFT)
    root.after(20, prog)
    root.mainloop()
    
    
    
    

    【讨论】:

    • 第一次调用循环时你还不如直接调用prog(),这也不是怎么做的,你应该这样做:root.after(20, prog)。问题是现在它无论如何都会立即执行,而且这样做是没有意义的。您可以只使用 prog()root.after(20, prog)
    • 您还应该将choice == Truechoice == False 行修正为choice = Truechoice = False 并在ja()nee() 中添加回global choice
    【解决方案2】:

    这行得通:

    import tkinter as tk
    global choice
    choice=False
    def ja():
        global choice
        choice=True
        print("Yes works")    # To check if this works
    
    def nee():
        global choice
        choice=False
        print("No works")    # To check if this works
    
    def prog():
        if choice:
            print("Prog works")
            # Program here
        root.after(20,prog)
                
    root = tk.Tk()
    root.title("Bot")
    label1 = tk.Label(root, text="continue?", width=75, height=25)
    Yes = tk.Button(root, text="Yes", width=25, height=5, command=ja)
    No = tk.Button(root, text="No", width=25, height=5, command=nee)
    Stop= tk.Button(root, text="Stop", width=25, height=5, command=lambda: root.destroy())
    
    label1.pack()
    Yes.pack(fill=tk.Y, side=tk.LEFT)
    No.pack(fill=tk.Y, side=tk.LEFT)
    Stop.pack(fill=tk.Y, side=tk.LEFT)
    root.after(20, prog)
    

    【讨论】:

      猜你喜欢
      • 2012-04-30
      • 1970-01-01
      • 1970-01-01
      • 2019-05-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-31
      相关资源
      最近更新 更多