在wx.wizard中使用wx.gauge

杰森·皮埃尔蓬(Jason Pierrepont)

我在使向导的第二页中的仪表显示和更新时遇到麻烦。直到达到100%,它才会显示。在我看来,该EVT_WIZARD_PAGE_CHANGED事件在显示页面上的对象之前已得到处理。

下面的代码是我正在尝试做的简化版本。当我运行它时,第二页只是挂起,直到fill_gauge方法完成并且量规为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()

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)

    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)

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()
约里兹

您更新量规的方法阻止了gui的事件循环。您可以使用wx.Timer来调用更新,以便它不会被阻止。请参见以下修改后的代码示例。

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()

注意:如果您返回上一页,则需要添加更好的代码来停止计时器并重置仪表,但我只是使它基本起作用。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用wx.wizard配置菜单

来自分类Dev

在wx.Panel中的wx.Frame中使用变量

来自分类Dev

wxPython wx.CallAfter()

来自分类Dev

Wx Matplotlib事件处理

来自分类Dev

使用WX显示文件路径

来自分类Dev

如何使用wx.python演示代码

来自分类Dev

wxPython wx.ScrolledWindow插入wx.Panel

来自分类Dev

wxPython wx.Frame和wx.Dialog效果

来自分类Dev

我在Windows上使用wxPython时遇到此错误“ import wx import error no module named wx”

来自分类Dev

锁定wx.stc.StyledTextCtrl

来自分类Dev

WX StaticBoxSizer / Sizer边框宽度

来自分类Dev

继承wx.grid.GridCellTextEditor

来自分类Dev

WxPython:导入wx.lib

来自分类Dev

在 wx.Panel 中滚动

来自分类Dev

wx python 图像质量下降

来自分类Dev

将 wx.grid 传递给 wx.frame WX.python

来自分类Dev

在Python wx.python中使用变量名中的占位符

来自分类Dev

如何在Python中使用wx.lib.plot设置轴限制?

来自分类Dev

Python wx导入使用Anaconda的Mac OS X 10.9.5失败

来自分类Dev

wxPython在wx.Window中如何使用不同的大小

来自分类Dev

持久绘图 + 使用 wx.PaintDC 临时覆盖

来自分类Dev

使用 wx.Simplebook 给我退出代码 3221225477

来自分类Dev

wxPython wx.lib.plot.PlotCanvas错误

来自分类Dev

wx.BoxSizer以垂直和水平居中

来自分类Dev

Python wx.listctrl滚动位置

来自分类Dev

填充wx.Choice的最快方法?

来自分类Dev

(wx)Maxima:`makelist`是并行还是串行工作?

来自分类Dev

wxPython wx.TextCtrl是不可编辑的

来自分类Dev

PyQT相当于wx CallAfter吗?