0
我需要在我的wxpython應用程序中包含一個進度條,但我發現的示例使用了一個從固定時間長度開始倒計時的計時器。由於我不知道需要多長時間才能讓給定的計算機運行我的過程,因此我希望進度欄只需在每個特定步驟完成時進行更新即可。wx.ProgressDialog使用計數器而不是計時器
我修改了一些示例代碼來實現這一點,但它引發以下錯誤:
path/ProgressDialog.py", line 31, in OnTimer
(keepGoing, skip) = self.dialog.Update(self.count)
File "C:\Python26\lib\site-packages\wx-2.8-msw-unicode\wx\_windows.py", line 2971, in Update
return _windows_.ProgressDialog_Update(*args, **kwargs)
PyAssertionError: C++ assertion "value <= m_maximum" failed at ..\..\src\generic\progdlgg.cpp(337) in wxProgressDialog::Update(): invalid progress value
當我添加的嘗試......除了在下面的代碼語句時,它不會拋出錯誤,但我在想,這樣做比起簡單地在錯誤信息中畫一個無花果葉更好。
任何人都可以告訴我如何解決我的代碼?
我的代碼如下,包括嘗試......除了聲明說:「刪除」錯誤:
import wx
import time
class Frame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, title="ProgressDialog sample")
self.progressMax = 7
self.count = 0
self.dialog = None
#self.timer = wx.Timer(self)
#self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
#self.timer.Start(1000)
self.OnTimer(self.count)
def OnTimer(self, evt):
try:
if not self.dialog:
self.dialog = wx.ProgressDialog("A progress box", "Time remaining",
self.progressMax,
style=wx.PD_CAN_ABORT
| wx.PD_ELAPSED_TIME
| wx.PD_REMAINING_TIME
| wx.PD_ESTIMATED_TIME
| wx.PD_AUTO_HIDE)
while self.count < 8:
self.count += 1
if wx.VERSION < (2,7,1,1):
keepGoing = self.dialog.Update(self.count)
else:
(keepGoing, skip) = self.dialog.Update(self.count)
time.sleep(2)
if not keepGoing or self.count == self.progressMax:
self.dialog.Destroy()
#self.timer.Stop()
except:
pass
if __name__ == "__main__":
app = wx.PySimpleApp()
frame = Frame(None)
frame.Show()
app.MainLoop()
注:我使用的是while循環來模擬通過工序步進,而我我正在測試這個代碼。但在實際執行中,我將每個self.count增加1