2011-05-19 97 views
2

嗨,我想知道你是否可以做到這一點,所以你可以有更多的一個按鈕按下一次?按下多個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() 

和我將能夠點擊「開始」,然後「走喜」,但我不能,因爲窗口凍結,直到它完成。是否有人知道如何做到這一點,以便您可以一次按下更多的按鈕?

回答

2

你在這裏想要的是使用線程。線程允許你在同一時間執行多個代碼片斷(或他們將至少出現被同時執行)

裏面的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