Python tkinter mainloop 在关闭窗口时不会退出 [英] Python tkinter mainloop not quitting on closing the window
问题描述
我是编程和 Python 的新手,正在制作一个用于练习的计算器应用程序.我在 Python 2.7 中使用 Tkinter.该应用程序具有您所期望的各种按钮,以及用于显示数字/结果的条目和标签小部件.
I'm new to programming and Python, and am making a calculator app to practise. I'm using Tkinter with Python 2.7. The app has various buttons as you'd expect, and Entry and Label widgets for showing the numbers/result.
我认为我的程序确实启动了主循环,但关闭窗口并不会停止主循环.由于在我添加 after 循环之前它确实正常工作,我认为 after 是问题所在.我也在使用 wait_variable.
I think my program does start mainloop, but closing the window doesn't stop mainloop. Since it did work as normal before I added an after loop, I assume the after is the problem. I'm also using wait_variable.
如果您能查看我的代码并提供一些建议,我将不胜感激!我已经包括了主要的东西;创建小部件和处理用户输入/结果输出的代码位于不同的文件(按钮、显示、输入)中,但希望没有这些文件也能理解.
I'd be very grateful if you could have a look at my code and give some advice! I've included the main stuff; the code to create the widgets and deal with user input/results output are in different files (Buttons, Displays, Inputs) but hopefully it's understandable without those.
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方法,因此我可以退出所有内容而无需运行主循环.self.end 函数有一个方法,我添加到 MainWindow 类中,该方法打印现在关闭",然后退出或销毁程序.
As I suppose I've made my own event handler instead of using mainloop, I did try adding self.parent.protocol("WM_DELETE_WINDOW", self.end(parent))
to the take_inputs method, so I could quit everything without needing to run mainloop. The self.end function there was a method I added to MainWindow class that printed "closing now" and then exited or destroyed the program.
然而,我为 protocol
放入的任何函数都会立即运行;WM_DELETE_WINDOW"
没有被正确查找(用foo"替换WM_DELETE_WINDOW"没有给出错误).
However, whatever function I put in there for protocol
was run immediately; "WM_DELETE_WINDOW"
wasn't being looked up properly (replacing "WM_DELETE_WINDOW" with "foo" didn't give an error).
感谢您的帮助!
推荐答案
我重写了代码,没有使用 wait_variable.我想问题是关闭窗口没有通过 wait_variable 所以代码永远不会回到主循环.
I've rewritten the code without using wait_variable. I suppose the problem was that closing the window did not pass wait_variable so the code never got back to the mainloop.
这是新的 take_inputs 方法.button.button_go
是一个布尔变量,首先定义为 False,但作为按钮绑定到鼠标单击(未显示)的一部分设置为 True.
Here's the new take_inputs method. button.button_go
is a Boolean variable first defined as False, but set to True as part of the button binding to the mouse click (not shown).
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 感到羞耻.听起来不错,但显然不是很有用.
The after() method now works nicely with mainloop as expected. Shame about wait_variable. It sounded neat but apparently isn't very useful.
我不知道为什么 wm_protocol "WM_DELETE_WINDOW" 定义不起作用!
I don't know why the wm_protocol "WM_DELETE_WINDOW" definition didn't work!
这篇关于Python tkinter mainloop 在关闭窗口时不会退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!