按下多个Tkinter按钮

问题描述:

嗨,我想知道你是否可以做到这一点,所以你可以有更多的一个按钮按下一次?按下多个Tkinter按钮

像:

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() 

和我将能够点击“开始”,然后“走喜”,但我不能,因为窗口冻结,直到它完成。是否有人知道如何做到这一点,以便您可以一次按下更多的按钮?

你在这里想要的是使用线程。线程允许你在同一时间执行多个代码片断(或他们将至少出现被同时执行)

里面的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的内置线程模块,这将是有益的。

+0

只有一件事,虽然我认为进口线应该在所有进口 – 2011-05-19 03:22:10