2011-07-04 31 views

回答

0

只是我做了什麼的一個基本例子,因爲指望Constantinius指出Thread與Tk一起工作!

import sys, thread 
from Tkinter import * 
from os import system as run 
from time import sleep 

r = Tk() 
r.title('Remote Support') 
t = StringVar() 
t.set('Completing Remote Support Initalisation   ') 
l = Label(r, textvariable=t).pack() 
def quit(): 
    #do cleanup if any 
    r.destroy() 
but = Button(r, text='Stop Remote Support', command=quit) 
but.pack(side=LEFT) 

def d(): 
    sleep(2) 
    t.set('Completing Remote Support Initalisation, downloading, please wait   ') 
    run('sleep 5') #test shell command 
    t.set('Preparing to run download, please wait   ') 
    run('sleep 5') 
    t.set("OK thanks! Remote Support will now close   ") 
    sleep(2) 
    quit() 

sleep(2) 
thread.start_new_thread(d,()) 
r.mainloop() 
1

標準線程庫應該完全沒問題,如果你用Tk運行它。 This source說,你應該讓主線程運行gui併爲你的os.system()調用創建線程。

你可以這樣寫它在完成任務的更新您的GUI的抽象:

def worker_thread(gui, task): 
    if os.system(str(task)) != 0: 
     raise Exception("something went wrong") 
    gui.update("some info") 

的線程可以使用thread.start_new_thread(function, args[, kwargs])標準庫的開始。請參閱文檔here

+0

謝謝君士坦丁,我會檢查一下! –

+0

Constantinius感謝您的輸入我在這裏有一個完整的例子:http://pastebin.com/AD0UAuMS如果您將它包含在您的答案中,它將會很棒。 –

相關問題