Python tkinter mainloop在关闭风的时候没有退出

2024-04-25 22:41:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我是编程和Python的新手,正在制作一个计算器应用程序来练习。 我在Python2.7中使用Tkinter。该应用程序有各种各样的按钮,以及显示数字/结果的条目和标签小部件。在

我想我的程序确实启动了mainloop,但是关闭窗口并不能停止mainloop。因为在我添加after循环之前它的工作正常,所以我假设after是问题所在。我也在使用wait_变量。在

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

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,所以我确实尝试在take_inputs方法中添加self.parent.protocol("WM_DELETE_WINDOW", self.end(parent)),这样我就可以不必运行mainloop就可以退出所有操作了。这个自行结束函数我在MainWindow类中添加了一个方法,该方法打印“立即关闭”,然后退出或销毁程序。在

但是,无论我为protocol插入的任何函数都会立即运行;"WM_DELETE_WINDOW"没有被正确地查找(用“foo”替换“WM_DELETE_WINDOW”没有给出错误)。在

谢谢你的帮助!在


Tags: theimportselfinputasdisplaybuttonframe
1条回答
网友
1楼 · 发布于 2024-04-25 22:41:21

我在没有使用wait_变量的情况下重写了代码。我想问题是关闭窗口并没有传递wait峎变量,所以代码再也没有回到主循环。在

这是一个新的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配合使用。为等待变量感到羞耻。听起来很整洁,但显然不是很有用。在

我不知道为什么wm_协议“wm_DELETE_WINDOW”的定义不起作用!在

相关问题 更多 >