在tkin中生成一个click事件

2024-10-01 11:19:48 发布

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

我正在试我的tkitner GUI。在

因此,我尝试从一个单独的线程生成click事件。 下面是一个测试Tkinter.按钮公司名称:

import unittest, threading
from Tkinter import *

class clickThread(threading.Thread): 
    def __init__(self, root): 
        threading.Thread.__init__(self)
        self.root = root 

    def run(self): 
        button = filter(lambda a: isinstance(a, Button), self.root.children.values())[0]
        print button
        button.focus()
        button.event_generate("<Button-1>")
        button.event_generate("<ButtonRelease-1>")
        print "clicked"

class Test(unittest.TestCase):
    def testName(self):
        root = Tk()
        button = Button(root, command=self.returnEvent)
        button.pack()
        thread = clickThread(root)
        thread.start()
        root.mainloop()

    def returnEvent(self):
        print "!"

方法测试返回事件不是由我生成的单击事件调用的。但如果我真的按一下,它会像预期的那样工作。在


Tags: importselfinittkinterdef事件buttonroot
1条回答
网友
1楼 · 发布于 2024-10-01 11:19:48

如果我没记错的话(我可能已经有好几年没试过了)光标需要在按钮上才能启动绑定。在

你知道按钮的“调用”方法吗?你可以用它来模拟巴顿的按压。在

相关问题 更多 >