【问题标题】:press more than one Tkinter button按多个 Tkinter 按钮
【发布时间】:2011-05-19 02:40:56
【问题描述】:

您好,我想知道您是否可以这样做,以便一次按下多个按钮?

喜欢:

from Tkinter import *
tkwin = Tk()
def delayedDoSomethings():
    for i in range(1,10000000):
        print 'hi',i
def delayedDoSomething():
       for i in range(1,10000000):
           print i

a = Button(tkwin, text="Go", command=delayedDoSomething)
a.pack()
b = Button(tkwin, text="Go hi", command=delayedDoSomethings)
b.pack()
tkwin.mainloop()

我可以点击“go”然后“go hi”,但我不能,因为窗口在完成之前会冻结。有没有人知道怎么做,这样你就可以一次按下多个按钮?

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    您在这里想要的是使用线程。线程允许您同时执行多段代码(或者它们至少会看起来同时执行)

    delayedDoSomethings() 内部,您需要生成一个执行实际工作的新线程,以便您可以在主线程中将控制权返回给 Tkinter。

    你会在delayedDoSomething()做同样的事情。

    这里有一些你可以在delayedDoSomethings()中使用的实际代码

    def delayedDoSomethings():
        def work():
            for i in rance(1, 10000000):
                print 'hi',i
        import thread
        thread.start_new_thread(separateThread, ()) #run the work function in a separate thread.
    

    Here 是 Python 内置线程模块的文档,会很有用。

    【讨论】:

    • 只有一件事,尽管我认为导入线程应该与所有导入一起位于顶部
    猜你喜欢
    • 2020-11-29
    • 2021-07-08
    • 1970-01-01
    • 2016-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多