wx.ProgressDialog未更新进度条或新消息

0 投票
3 回答
1198 浏览
提问于 2025-04-16 06:15

wx.ProgressDialog的更新方法有一个叫做newmsg的参数,本来是用来在每一步过程中给出文本更新的,但我的代码没有正确实现这个功能。

这里是wx.ProgressDialog的文档链接 http://www.wxpython.org/docs/api/wx.ProgressDialog-class.html

另外,当我运行我的代码时,进度条在大约完成50%时就停止更新了。

有没有人能告诉我怎么解决这两个问题?

这是我的代码:

import wx
import time

class Frame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, title="ProgressDialog sample")
        self.progressMax = 4
        self.count = 0
        self.newstep='step '+str(self.count)
        self.dialog = None
        self.OnTimer(self.count)

    def OnTimer(self, evt):
        if not self.dialog:
            self.dialog = wx.ProgressDialog("Progress in processing your data.",
                                        self.newstep,
                                        self.progressMax,
                                        style=wx.PD_CAN_ABORT
                                        | wx.PD_APP_MODAL
                                        | wx.PD_SMOOTH
                                        | wx.PD_AUTO_HIDE)
        # Do Step One
        print '----------------------------'
        print 'Starting Step One now.'
        self.count += 1
        self.newstep='step '+str(self.count)
        print 'self.count is:  ',self.count
        print 'self.newstep is:  ',self.newstep
        keepGoing = self.dialog.Update(self.count,self.newstep)
        print '----------------------------'
        time.sleep(5)

        # Do Step Two
        print '----------------------------'
        print 'Starting Step Two now.'
        self.count += 1
        self.newstep='step '+str(self.count)
        print 'self.count is:  ',self.count
        print 'self.newstep is:  ',self.newstep
        keepGoing = self.dialog.Update(self.count,self.newstep)
        print '----------------------------'
        time.sleep(5)

        # Do Step Three
        print '----------------------------'
        print 'Starting Step Three now.'
        self.count += 1
        self.newstep='step '+str(self.count)
        print 'self.count is:  ',self.count
        print 'self.newstep is:  ',self.newstep
        keepGoing = self.dialog.Update(self.count,self.newstep)
        print '----------------------------'
        time.sleep(5)

        # Do Step Four
        print '----------------------------'
        print 'Starting Step Four now.'
        self.count += 1
        self.newstep='step '+str(self.count)
        print 'self.count is:  ',self.count
        print 'self.newstep is:  ',self.newstep
        keepGoing = self.dialog.Update(self.count,self.newstep)
        print '----------------------------'
        time.sleep(5)

        # Delete the progress bar when it is full
        self.dialog.Update(self.progressMax)
        time.sleep(3)
        self.dialog.Destroy()

if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = Frame(None)
    frame.Show()
    app.MainLoop()

注意我在打印所有内容以检查进度。打印的结果和进度对话框中显示的内容不同。打印命令似乎按照代码的指示在运行,但进度对话框却没有按照代码的指示在更新,进度对话框的结果和打印命令的结果不一致。
这是在Python 2.6版本中。

编辑一:

-------------------------------------------------------------------------------

我根据adw的建议修改了上面的代码。newmsg不更新的问题似乎解决了,但进度条仍然只显示50%满,这发生在对话框中的newmsg输出显示“步骤3”时。然后进度条就消失了。一个不懂电脑的人使用这个软件可能会认为这个过程只完成了大约50%的工作,因为在步骤3时就提前结束了。我该如何修改代码,让对话框显示“步骤4”,并且让进度条在关闭之前实际填满到100%持续一两秒呢?

编辑二:

------------------------------------------------------------------------------

我根据ChrisC的建议对上面的代码进行了修改,如你所见。但运行这个新修改的代码仍然出现同样的问题。所以这个建议似乎没有在我理解的情况下有效。你能给我一些具体的建议吗?有没有什么方法能让它在你的电脑上正常工作?

3 个回答

0

你能在 self.dialog.Destroy() 之前调用 self.dialog.Update(self.progressMax) 吗?可以加一个 time.sleep(1) 来让它暂停一下,这样能看得更清楚吗?

1

在Python中,变量名是区分大小写的,所以 newstepnewStep 是两个不同的东西。

至于你提到的那个条形图,我运行你的代码时它工作得很好。不过,我需要把 (keepGoing, skip) 改成 keepGoing,可能是因为版本不同(我用的是 wx.VERSION_STRING == '2.6.3.2')。

1

你的问题出在 wx.PD_AUTO_HIDE 这个设置上。根据说明书的说法,使用这个设置会导致进度对话框在进度条达到最大值时立即消失。

也就是说,一旦进度条满了,进度对话框就会从屏幕上消失。

你可以把代码改成

self.dialog = wx.ProgressDialog("Progress in processing your data.",
                                    self.newstep,
                                    self.progressMax,
                                    style=wx.PD_CAN_ABORT
                                    | wx.PD_APP_MODAL
                                    | wx.PD_SMOOTH)

这样的话,ChrisC 提出的代码就能正常工作了。

撰写回答