首页 文章

关闭窗口时,Python tkinter mainloop不会退出

提问于
浏览
2

我是编程和Python的新手,我正在制作一个计算器应用程序来练习 . 我正在使用Tkinter和Python 2.7 . 该应用程序具有您期望的各种按钮,以及用于显示数字/结果的Entry和Label小部件 .

我认为我的程序确实启动了mainloop,但关闭窗口并不会停止mainloop . 因为在添加后循环之前它确实正常工作,所以我认为后面是问题 . 我也在使用wait_variable .

如果您能看一下我的代码并给出一些建议,我将非常感激!我把主要内容包括在内;创建小部件和处理用户输入/结果输出的代码在不同的文件(按钮,显示,输入)中,但希望没有这些可以理解 .

import Tkinter as tk
# These contain the other bits of code
import Buttons as bt
import Displays as ds
import Inputs as ip


class MainWindow(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.parent = parent
        self.parent.title("Calculator")

        # Making frames to fill with widgets
        self.displays_frame = tk.Frame(parent)
        self.displays_frame.grid(padx=10, pady=10)
        self.buttons_frame = tk.Frame(parent)
        self.buttons_frame.grid()
        # Initialising the widgets and user input functions
        self.display = ds.Displays(self.displays_frame)
        self.button = bt.Button(self.buttons_frame)
        self.input = ip.Inputs()

    def take_inputs(self, parent):
        self.parent = parent

        # This waits for a button to be pressed
        self.wait_variable(self.button.button_pressed)
        # On a button press, its value is inserted into display Entry box/result is put in Label
        # Both self.button.button_pressed above and self.display.r below are StringVars
        self.input.process_input(self.button.button_pressed, self.display.entry_box, self.display.r)
        # Using after here so it keeps waiting for button presses to be recorded
        self.after(100, self.take_inputs, self.parent)

def main():
    root = tk.Tk()
    app = MainWindow(root)
    # Here the wait_variable and after functions are called
    app.take_inputs(root)
    # The below string is printed after the first button has been pressed
    print "main, now starting mainloop" 
    root.mainloop()
    # "finished" is never printed
    print "finished"

if __name__ == '__main__':
    main()

我假设我已经创建了自己的事件处理程序而不是使用mainloop,我尝试将 self.parent.protocol("WM_DELETE_WINDOW", self.end(parent)) 添加到take_inputs方法中,因此我可以在不需要运行mainloop的情况下退出所有内容 . self.end函数有一个我添加到MainWindow类的方法,它打印"closing now"然后退出或销毁该程序 .

但是,我为 protocol 放置的任何功能都立即运行; "WM_DELETE_WINDOW" wasn 't being looked up properly (replacing 2385958 with 2385959 didn' t给出错误) .

谢谢你的帮助!

1 回答

  • 0

    我在不使用wait_variable的情况下重写了代码 . 我想问题是关闭窗口没有通过wait_variable所以代码永远不会回到mainloop .

    这是新的take_inputs方法 . button.button_go 是一个布尔变量,首先定义为False,但设置为True作为绑定到鼠标单击的按钮的一部分(未显示) .

    def take_inputs(self, parent):
        self.parent = parent
    
        # button.button_go is immediately set to False on updating the display box
        if self.button.button_go == True:
            self.input.process_input(self.button, self.display.entry_box, self.display.r)
            self.button.button_go = False
        self.after(100, self.take_inputs, self.parent)
    

    after()方法现在可以正常使用mainloop . 关于wait_variable的耻辱 . 它听起来很整洁,但显然不是很有用 .

    我不知道为什么wm_protocol“WM_DELETE_WINDOW”定义不起作用!

相关问题