问题描述
我看过的每个 tkinter 教程都声称必须调用 tkinter.mainloop
来绘制窗口和处理事件,并且它们总是调用这个函数,即使在 hello world 程序中也是如此.但是,当我在交互式 shell 中尝试这些时,无需调用 mainloop 即可正确绘制窗口.这个例子在 tkinter 中嵌入 matplotlib 图形产生了一个相对复杂的应用程序,带有用于在 tkinter 窗口中平移、缩放和调整绘图大小,同样,如果您删除对 mainloop 的调用并在交互式 shell 中运行代码,这一切都有效.当然,如果我在交互式 shell 之外运行脚本(删除了 mainloop),程序结束得太快而看不到会发生什么,但是如果我添加对 input
的调用以保持程序打开,一切正常正确(我在 linux 上运行 python 3.2.2).
Every tkinter tutorial I have seen claims that tkinter.mainloop
must be called for windows to be drawn and events to be processed, and they always call this function, even in hello world programs. However, when I try these out in the interactive shell, windows are drawn correctly without having to call mainloop. This example of embedding matplotlib graphics in tkinter produces a relatively complex application, with buttons for panning, zooming and resizing a plot within a tkinter window, and again, this all works if you remove the call to mainloop and run the code in the interactive shell. Of course, if I run the script (with mainloop removed) outside the interactive shell, the program ends too quickly to see what happens, but if I add a call to input
to hold the program open everything works correctly (I'm running python 3.2.2 on linux).
那么mainloop到底是做什么的,什么时候需要调用呢?
So what exactly does mainloop do, and when is it necessary to call it?
澄清一下,如果我打开 GNOME 终端并输入
To clarify, if I open up the GNOME terminal and type
$python3
>>> import tkinter
>>> root = tkinter.Tk()
一个窗口立即出现而无需调用 mainloop,更复杂的 tkinter 功能似乎也能工作(例如,向窗口添加按钮).在 IDLE 中,需要调用 mainloop.我的理解是,在调用 mainloop 之前,不应绘制任何内容,也不应处理任何事件.
a window immediately appears without having to call mainloop, and more complex tkinter functionality seems to work as well (for example, adding buttons to the window). In IDLE, a call to mainloop is necessary. It was my understanding that nothing should be drawn, and no events should be processed, until mainloop is called.
推荐答案
您的主要问题的答案是,当您准备好运行应用程序时,您必须调用一次且仅一次的 mainloop.
The answer to your main question is, you must call mainloop once and only once, when you are ready for your application to run.
mainloop
只不过是一个大致如下所示的无限循环(这些不是方法的实际名称,名称仅用于说明这一点):
mainloop
is not much more than an infinite loop that looks roughly like this (those aren't the actual names of the methods, the names merely serve to illustrate the point):
while True:
event=wait_for_event()
event.process()
if main_window_has_been_destroyed():
break
在这种情况下,事件"意味着用户交互(鼠标点击、按键等)以及来自工具包或操作系统/窗口管理器的绘制或重绘小部件的请求.如果该循环未运行,则不会处理事件.如果事件没有得到处理,屏幕上不会显示任何内容,除非您有自己的无限循环运行,否则您的程序可能会退出.
In this context, "event" means both the user interactions (mouse clicks, key presses, etc) and requests from the toolkit or the OS/window manager to draw or redraw a widget. If that loop isn't running, the events don't get processed. If the events don't get processed, nothing will appear on the screen and your program will likely exit unless you have your own infinite loop running.
那么,为什么不需要以交互方式调用它呢?这只是为了方便,否则一旦调用 mainloop
就不可能输入任何命令,因为 mainloop
一直运行到主窗口被销毁.
So, why don't you need to call this interactively? That's just a convenience, because otherwise it would be impossible to enter any commands once you call mainloop
since mainloop
runs until the main window is destroyed.
这篇关于我什么时候需要在 Tkinter 应用程序中调用 mainloop?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!