Tkinter:在主循環中調用事件

[英]Tkinter: invoke event in main loop


How do you invoke a tkinter event from a separate object?

如何從單獨的對象調用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方法並從我的單獨對象中檢查狀態,但這似乎很浪費。

2 个解决方案

#1


27  

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的方法,而不會鎖定應用程序。這告訴我你的代碼中還有其他的東西。

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)

#2


4  

Here below just some doc and link to better understand Bryan's answer above.

下面是一些文檔和鏈接,以更好地了解Bryan的上述答案。

function description from New Mexico Tech :

功能描述來自新墨西哥州技術:

w.event_generate(sequence, **kw)

w.event_generate(序列,** 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.

此方法會導致事件在沒有任何外部刺激的情況下觸發。事件的處理與外部刺激觸發的情況相同。 sequence參數描述了要觸發的事件。您可以通過提供keyword = value參數為Event對象中的選定字段設置值,其中關鍵字指定Event對象中字段的名稱。

list and description of tcl/tk event attributes here

這里列出和描述tcl / tk事件屬性


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2008/11/06/56535c08b9e97ddd3403344555d9d186.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com