在 wxPython 中,可以使用 wx.wizard 实现一个包含进度条的向导界面。以下是一个实现示例,该示例创建了一个向导窗口,并在每一步显示一个进度条,模拟任务进度更新。
1、问题背景
在使用wx.wizard进行向导式页面设计时,在第二个页面中添加的进度条无法正常显示和更新。在进度条达到100%之前,它一直不会显示。这似乎是因为EVT_WIZARD_PAGE_CHANGED事件在页面上的对象被显示之前就被处理了。
2、解决方案
为了解决这个问题,可以使用wx.Timer来调用更新进度条的函数,从而避免阻塞GUI的事件循环。更新进度条的函数可以每隔一段时间更新进度条的进度值和状态,并在进度条达到100%时停止更新。
这里提供了一个修改后的代码示例:
import time
import wx.wizard
class Wizard(wx.wizard.Wizard):
def __init__(self, parent, title):
wx.wizard.Wizard.__init__(self, parent, wx.ID_ANY, title)
self.pages = []
self.Bind(wx.wizard.EVT_WIZARD_PAGE_CHANGED, self.on_page_changed)
def add_page(self, page):
"""Add a WizardPage to the pages list"""
self.pages.append(page)
def chain_pages(self):
i = 0
while 1:
if i == len(self.pages) - 1:
break
else:
wx.wizard.WizardPageSimple_Chain(self.pages[i], self.pages[i + 1])
i += 1
def run(self):
self.RunWizard(self.pages[0])
def on_page_changed(self, evt):
# if coming from self.pages[0]
# and direction is forward
if evt.GetDirection():
direction = 'forward'
else:
direction = 'backward'
if evt.GetPage() is self.pages[1] \
and direction == "forward":
# self.pages[1].fill_gauge()
self.pages[1].timer.Start(1000)
class StartPage(wx.wizard.WizardPageSimple):
def __init__(self, parent, title):
wx.wizard.WizardPageSimple.__init__(self, parent)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.text = wx.StaticText(self, -1,
"This is the First Page")
# self.text.Wrap(parent.GetClientSizeTuple()[0])
self.sizer.Add(self.text, 0, wx.ALIGN_CENTER | wx.ALL, 5)
class UpdatePage(wx.wizard.WizardPageSimple):
def __init__(self, parent, title):
wx.wizard.WizardPageSimple.__init__(self, parent)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.status = wx.StaticText(self, -1, "This is the Second Page")
self.gauge = wx.Gauge(self, -1, name="Guage")
self.sizer.Add(self.status, 0, wx.ALIGN_CENTER | wx.ALL, 5)
self.sizer.Add(self.gauge, 0,
wx.EXPAND | wx.ALIGN_CENTER_HORIZONTAL | wx.ALL, 10)
self.SetSizer(self.sizer)
self.gauge_pos = 0 # Added
self.timer = wx.Timer(self) # Added
self.Bind(wx.EVT_TIMER, self.on_gauge_timer) # Added
def update(self, percent, status):
self.status.SetLabel(status)
self.gauge.SetValue(percent)
# self.Refresh()
# def fill_gauge(self):
# x = 0
# while x <= 100:
# self.update(x, "Gauge is at %d" % x)
# x += 10
# time.sleep(1)
def on_gauge_timer(self, event): # Added method
if self.gauge_pos < 100:
self.gauge_pos += 10
self.update(self.gauge_pos, "Gauge is at %d" % self.gauge_pos)
else:
self.timer.Stop()
self.gauge_pos = 0
if __name__ == '__main__':
app = wx.App()
wizard = Wizard(None, "Updater")
wizard.add_page(StartPage(wizard, "Updater"))
wizard.add_page(UpdatePage(wizard, "Updater"))
wizard.chain_pages()
wizard.run()
wizard.Destroy()
app.MainLoop()
在这个代码示例中,使用了wx.Timer来每隔1秒更新进度条的进度值和状态。当进度条达到100%时,定时器会自动停止,并且进度条会被重置。这样,进度条就可以在第二个页面上正常显示和更新了。
- 这里使用
time.sleep模拟任务进度更新。实际应用中可以用线程或后台任务来更新进度,以免阻塞主线程的 GUI。 - 可以根据需要调整进度条更新速度或进度上限(
max_progress)。