python - 使用 wxPython 显示进度

标签 python python-2.7 wxpython

我希望有人能够帮助我使用 wxPython 显示长时间运行的任务的进度。

我目前有一个函数,由按钮调用,并执行 5 个其他函数,例如

def sum(self, event):
    self.processSents()
    self.processWords()
    self.repSents()
    self.measSim()
    self.selCont()

我想在执行这些函数时显示进度条,因为程序有时会挂起,这并不理想。

我看过的很多解决方案都建议使用线程,但我对 Python 中的线程非常缺乏经验,而且我的尝试却没有取得任何进展。例如,我不确定对话框是否应该位于线程、执行代码或两者中。

我目前的尝试如下:

def sum(self, event):
    progress = wx.ProgressDialog("sum in progress", "please wait", maximum=100, parent=self, style=wx.PD_SMOOTH|wx.PD_AUTO_HIDE)
    self.Start(self.DoWork, progress)
    progress.ShowModal()

def Start(func, *args):
    thread = threading.Thread(target=func, args=args)
    thread.setDaemon(True)
    thread.start()

def DoWork(self, progress):
    for i in range(101):
        ex.CallAfter(progress.Update, i)
        time.sleep(0.2)
    self.processSents()
    self.processWords()
    self.repSents()
    self.measSim()
    self.selCont()
    wx.CallAfter(progress.Destroy)

到目前为止我看过的解决方案是:

Updating a wxPython progress bar after calling app.MainLoop()

How to thread wxPython progress bar

How Can I Safely Manage wxPython Progress Dialog Threading?

http://wiki.wxpython.org/LongRunningTasks

任何帮助或建议将不胜感激,因为我很迷失:(

谢谢

克里斯

更新到工作版本(Jerry 的响应与 Corley 推荐的 wx.Yield() 相结合

def sum(self, event):
    progress = wx.ProgressDialog("sum in progress", "please wait", maximum=100, parent=self, style=wx.PD_SMOOTH|wx.PD_AUTO_HIDE)
    self.processSents()
    percent = 20
    progress.Update(percent)
    self.processWords()
    percent += 20
    progress.Update(percent)

    // repSends, measSim and selCont omitted to save space
    progress.Destroy()

wx.Yield() 从每个函数中调用,例如

def processSents(self):
    // some long running process
    wx.Yield()
    // end of long running process

最佳答案

1) 单击按钮时将创建线程 DoWork

2) 在DoWork中,将创建另一个线程showProgress来显示进度对话框

3)在DoWork中,doSomething模拟一些耗时的事情

4) 在此示例中,将在每个 doSomething 之前创建另一个线程 updateProgress,以避免进度条卡住,但实际上您应该调用 self.progress。更新以在总进度

时更新进度条
import wx
import threading
import  time

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None)
        panel = wx.Panel(self)
        btn1 = wx.Button(panel, label="test1")
        btn1.Bind(wx.EVT_BUTTON, self.onButton1)

        btn2 = wx.Button(panel, label="test2")
        btn2.Bind(wx.EVT_BUTTON, self.onButton2)

        sizer = wx.BoxSizer(wx.VERTICAL)

        sizer.Add(btn1)
        sizer.Add(btn2)

        panel.SetSizer(sizer)


        self.maxPercent = 100
        self.percent = 0

    def onButton1(self, evt):
        self.StartThread(self.DoWork1)

    def onButton2(self, evt):
        self.StartThread(self.DoWork2)

    def StartThread(self, func, *args):
        thread = threading.Thread(target=func, args=args)
        thread.setDaemon(True)
        thread.start()

    def showProgress(self):
        self.progress = wx.ProgressDialog("sum in progress", "please wait", maximum=self.maxPercent, parent=self, style=wx.PD_SMOOTH|wx.PD_AUTO_HIDE)

    def destoryProgress(self):
        self.progress.Destroy()

    def updateProgress(self, percent):
        keepGoing = True
        time.sleep(1)
        while keepGoing and self.percent < percent:
            self.percent += 1
            (keepGoing, skip) = self.progress.Update(self.percent)
            time.sleep(0.1)


    def doSomething(self, take_time, taskPercent, say_something):
        time.sleep(take_time)
        (keepGoing, skip) = self.progress.Update(taskPercent, say_something+" done!")


    def DoWork1(self):
        self.StartThread(self.showProgress)

        taskPercent = 25
        self.StartThread(self.updateProgress, taskPercent)
        self.doSomething(5, taskPercent, "1st")

        taskPercent +=25
        self.StartThread(self.updateProgress, taskPercent)
        self.doSomething(5, taskPercent, "2nd")

        taskPercent +=25
        self.StartThread(self.updateProgress, taskPercent)
        self.doSomething(5, taskPercent, "3rd")

        taskPercent +=25
        self.StartThread(self.updateProgress, taskPercent)
        self.doSomething(5, taskPercent, "4th")

        self.destoryProgress()

    def DoWork2(self):
        self.StartThread(self.showProgress)

        taskPercent = 25
        self.doSomething(5, taskPercent, "1st")

        taskPercent +=25
        self.doSomething(5, taskPercent, "2nd")

        taskPercent +=25
        self.doSomething(5, taskPercent, "3rd")

        taskPercent +=25
        self.doSomething(5, taskPercent, "4th")

        self.destoryProgress()


if __name__ == '__main__':

    app = wx.App(0)
    frame = MyFrame()
    frame.Show()
    app.MainLoop()

关于python - 使用 wxPython 显示进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21995311/

相关文章:

python - Stripe 不在 Python 中抛出充电错误

python - 检测常见文件类型

python - 如何很好地测量 Pandas 数据框中相同数据的运行

bash - Google Colab 脚本抛出 "Transport endpoint is not connected"

python - wxpython在wxpython staticbitmap中刷新图像

python - Pandas 情节 : how to plot some columns of a dataframe with the same color but different style and some cols with different color and different style?

python - 我如何使用字符串来表示 sqlalchemy 对象属性?

python - 类型错误 : 'generator' object has no attribute '__getitem__'

python - 检查时间控制并更改 listrCtrl 中的颜色

python - 如何判断 `ComboBox` 的下拉列表是否打开?