wxPython进度条崩溃,无法按预期工作

时间:2014-07-03 06:45:43

标签: python wxpython

我正在尝试使用wx.Gauge实现一个简单的进度条。在转到原始代码之前,我想编写如下的小脚本:

import time
for i in range(101):
    print i
    time.sleep(0.5)

上面的脚本只是延迟了for循环。现在该程序的相同概念是悬挂我的Ubuntu 12.04LTS并最终显示完整的进度条。这是代码:

# -*- coding: utf-8 -*- 
import wx
import time

class MainFrame ( wx.Frame ):

    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"Progress Bar Example", pos = wx.DefaultPosition, size = wx.Size( 460,135 ), style = wx.CAPTION|wx.CLOSE_BOX|wx.TAB_TRAVERSAL )

        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )

        bSizer2 = wx.BoxSizer( wx.VERTICAL )

        self.progress = wx.Gauge( self, wx.ID_ANY, 100, wx.Point( 10,10 ), wx.Size( 400,-1 ), wx.GA_HORIZONTAL|wx.GA_SMOOTH )
        self.progress.SetValue( 0 ) 
        self.progress.SetForegroundColour( wx.Colour( 255, 255, 255 ) )

        bSizer2.Add( self.progress, 0, wx.ALL, 5 )

        self.start_again = wx.Button( self, wx.ID_ANY, u"START", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer2.Add( self.start_again, 0, wx.ALL, 5 )


        self.SetSizer( bSizer2 )
        self.Layout()

        self.Centre( wx.BOTH )

        # Connect Events
        self.start_again.Bind( wx.EVT_BUTTON, self.run_progreee_bar )

    def __del__( self ):
        pass


    # Virtual event handlers, overide them in your derived class
    def run_progreee_bar( self, event ):
        self.progress.SetValue( 0 )
        for i in range(101):
            self.progress.SetValue( i )
            self.delay()
    def delay(self):
        time.sleep(0.5)

app  = wx.App()
frame = MainFrame(None)
frame.Show()
app.MainLoop()

我相信程序在语法上是正确的。我认为逻辑也是正确的。我在这段代码中遇到了什么问题?

1 个答案:

答案 0 :(得分:1)

发生这种情况的原因是你的计数器比app.MainLoop()可以重绘进度条的速度快。因此,如果您在按下启动后尝试与窗口小部件进行交互,则会挂起,直到循环结束并显示最终状态。您需要使用线程在GUI编程中执行这些任务。我改变了你的代码。它现在应该可以正常工作。

import wx, time, thread

class MainFrame ( wx.Frame ):

    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"Progress Bar Example", pos = wx.DefaultPosition, size = wx.Size( 460,135 ), style = wx.CAPTION|wx.CLOSE_BOX|wx.TAB_TRAVERSAL )

        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )

        bSizer2 = wx.BoxSizer( wx.VERTICAL )

        self.progress = wx.Gauge( self, wx.ID_ANY, 100, wx.Point( 10,10 ), wx.Size( 400,-1 ), wx.GA_HORIZONTAL|wx.GA_SMOOTH )
        self.progress.SetValue( 0 ) 
        self.progress.SetForegroundColour( wx.Colour( 255, 255, 255 ) )

        bSizer2.Add( self.progress, 0, wx.ALL, 5 )

        self.start_again = wx.Button( self, wx.ID_ANY, u"START", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer2.Add( self.start_again, 0, wx.ALL, 5 )


        self.SetSizer( bSizer2 )
        self.Layout()

        self.Centre( wx.BOTH )

        # Connect Events
        self.start_again.Bind( wx.EVT_BUTTON, self.run_progreee_bar)

    def __del__( self ):
        pass


    # Virtual event handlers, overide them in your derived class
    def run_progreee_bar( self, event ):
        thread.start_new_thread(self.update_progress_bar, ())

    def update_progress_bar(self):
        self.start_again.Disable()
        self.progress.SetValue( 0 )
        for i in range(101):
            self.progress.SetValue( i )
            self.delay()
        self.start_again.Enable()

    def delay(self):
        time.sleep(0.5)

app  = wx.App()
frame = MainFrame(None)
frame.Show()
app.MainLoop()

您可能还想阅读此article