python Tkinter:在主循环中调用事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/270648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tkinter: invoke event in main loop
提问by Mark
How do you invoke a tkinter event
from a separate object?
如何event
从单独的对象调用 tkinter ?
I'm looking for something like wxWidgets wx.CallAfter
. For example, If I create an object, and pass to it my Tk
root instance, and then try to call a method of that root window from my object, my app locks up.
我正在寻找类似 wxWidgets 的东西wx.CallAfter
。例如,如果我创建一个对象,并将我的Tk
根实例传递给它,然后尝试从我的对象调用该根窗口的方法,我的应用程序就会锁定。
The best I can come up with is to use the the after
method and check the status from my separate object, but that seems wasteful.
我能想到的最好after
方法是使用该方法并从我的单独对象中检查状态,但这似乎很浪费。
回答by Bryan Oakley
To answer your specific question of "How do you invoke a TkInter event from a separate object", use the event_generate
command. It allows you to inject events into the event queue of the root window. Combined with Tk's powerful virtual event mechanism it becomes a handy message passing mechanism.
要回答“如何从单独的对象调用 TkInter 事件”的特定问题,请使用该event_generate
命令。它允许您将事件注入到根窗口的事件队列中。结合 Tk 强大的虚拟事件机制,它成为一种方便的消息传递机制。
For example:
例如:
from tkinter import *
def doFoo(*args):
print("Hello, world")
root = Tk()
root.bind("<<Foo>>", doFoo)
# some time later, inject the "<<Foo>>" virtual event at the
# tail of the event queue
root.event_generate("<<Foo>>", when="tail")
Note that the event_generate
call will return immediately. It's not clear if that's what you want or not. Generally speaking you don't want an event based program to block waiting for a response to a specific event because it will freeze the GUI.
请注意,event_generate
调用将立即返回。目前尚不清楚这是否是您想要的。一般来说,您不希望基于事件的程序阻止等待对特定事件的响应,因为它会冻结 GUI。
I'm not sure if this solves your problem though; without seeing your code I'm not sure what your real problem is. I can, for example, access methods of root in the constructor of an object where the root is passed in without the app locking up. This tells me there's something else going on in your code.
不过,我不确定这是否能解决您的问题;没有看到您的代码,我不确定您的真正问题是什么。例如,我可以在对象的构造函数中访问 root 的方法,在该对象的构造函数中传入 root 而不锁定应用程序。这告诉我您的代码中还有其他事情发生。
Here's an example of successfully accessing methods on a root window from some other object:
下面是从其他对象成功访问根窗口上的方法的示例:
from tkinter import *
class myClass:
def __init__(self, root):
print("root background is %s" % root.cget("background"))
root = Tk()
newObj = myClass(root)
回答by user110954
Here below just some doc and link to better understand Bryan's answer above.
下面只是一些文档和链接,以更好地理解上面 Bryan 的回答。
function description from New Mexico Tech:
New Mexico Tech 的功能描述:
w.event_generate(sequence, **kw)
w.event_generate(sequence, **kw)
This method causes an event to trigger without any external stimulus. The handling of the event is the same as if it had been triggered by an external stimulus. The sequence argument describes the event to be triggered. You can set values for selected fields in the Event object by providing keyword=value arguments, where the keyword specifies the name of a field in the Event object.
此方法导致事件在没有任何外部刺激的情况下触发。事件的处理就像它是由外部刺激触发一样。序列参数描述要触发的事件。您可以通过提供 keyword=value 参数为 Event 对象中的选定字段设置值,其中关键字指定 Event 对象中字段的名称。
list and description of tcl/tk event attributes here
此处列出 tcl/tk 事件属性的列表和说明