1
我在下面的代碼有問題。當我運行它時,進度條會脈衝大約10秒鐘,然後繼續下載並顯示進度,但完成後它將不會移動到剛剛鎖定的下一步。Python四步設置與進度條
import sys
import time
import pygtk
import gtk
import gobject
import threading
import urllib
import urlparse
class WorkerThread(threading.Thread):
def __init__ (self, function, parent, arg = None):
threading.Thread.__init__(self)
self.function = function
self.parent = parent
self.arg = arg
self.parent.still_working = True
def run(self): # when does "run" get executed?
self.parent.still_working = True
if self.arg == None:
self.function()
else:
self.function(self.arg)
self.parent.still_working = False
def stop(self):
self = None
class MainWindow:
def __init__(self):
gtk.gdk.threads_init()
self.wTree = gtk.Builder()
self.wTree.add_from_file("gui.glade")
self.mainWindows()
def mainWindows(self):
self.mainWindow = self.wTree.get_object("frmMain")
dic = {
"on_btnNext_clicked" : self.mainWindowNext,
}
self.wTree.connect_signals(dic)
self.mainWindow.show()
self.installerStep = 0 # 0 = none, 1 = preinstall, 2 = download, 3 = install info, 4 = install
#gtk.main()
self.mainWindowNext()
def pulse(self):
self.wTree.get_object("progress").pulse()
if self.still_working == False:
self.mainWindowNext()
return self.still_working
def preinstallStep(self):
self.wTree.get_object("progress").set_fraction(0)
self.wTree.get_object("btnNext").set_sensitive(0)
self.wTree.get_object("notebook1").set_current_page(0)
self.installerStep = 1
WT = WorkerThread(self.heavyWork, self) #Would do a heavy function here like setup some thing
WT.start()
gobject.timeout_add(75, self.pulse)
def downloadStep(self):
self.wTree.get_object("progress").set_fraction(0)
self.wTree.get_object("btnNext").set_sensitive(0)
self.wTree.get_object("notebook1").set_current_page(0)
self.installerStep = 2
urllib.urlretrieve('http://mozilla.mirrors.evolva.ro//firefox/releases/3.6.3/win32/en-US/Firefox%20Setup%203.6.3.exe', '/tmp/firefox.exe', self.updateHook)
self.mainWindowNext()
def updateHook(self, blocks, blockSize, totalSize):
percentage = float (blocks * blockSize)/totalSize
if percentage > 1:
percentage = 1
self.wTree.get_object("progress").set_fraction(percentage)
while gtk.events_pending():
gtk.main_iteration()
def installInfoStep(self):
self.wTree.get_object("btnNext").set_sensitive(1)
self.wTree.get_object("notebook1").set_current_page(1)
self.installerStep = 3
def installStep(self):
self.wTree.get_object("progress").set_fraction(0)
self.wTree.get_object("btnNext").set_sensitive(0)
self.wTree.get_object("notebook1").set_current_page(0)
self.installerStep = 4
WT = WorkerThread(self.heavyWork, self) #Would do a heavy function here like setup some thing
WT.start()
gobject.timeout_add(75, self.pulse)
def mainWindowNext(self, widget = None):
if self.installerStep == 0:
self.preinstallStep()
elif self.installerStep == 1:
self.downloadStep()
elif self.installerStep == 2:
self.installInfoStep()
elif self.installerStep == 3:
self.installStep()
elif self.installerStep == 4:
sys.exit(0)
def heavyWork(self):
time.sleep(10)
if __name__ == '__main__':
MainWindow()
gtk.main()
我有一種感覺,它的東西用做:
while gtk.events_pending():
gtk.main_iteration()
是否有這樣做的更好的辦法?
你可以編輯它,所以它不是每間隔行間隔兩倍,標題用「for」而不是「4」? – 2010-05-16 21:29:03
4代表「四個」不代表「代表」,因爲它有4個步驟。你可以改變間距,它在發佈時並不是在編輯器中雙倍間隔的。 – 2010-05-17 08:17:03
嘿,等一下? Python 4已經出來,我仍然停留在Python 2上? ... 啊對。 :( – badp 2010-05-27 22:07:55