打开进度条直到功能任务结束

2024-10-03 15:27:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个事件侦听器在左双击wx.网格将打开一个对话框以确认更改。如果用户单击“是”,我会调用一个大约需要6秒才能执行的函数,同时窗口冻结。我想做的是打开一个进度条,这样用户就可以等到函数完成任务,或者更好的是,在对话框中显示进度条。在

我不知道从哪里开始,因为我从来都不需要进度条。在

我看过的很多解决方案都建议使用线程,但我对Python中的线程还很缺乏经验。在

我希望有人能帮我用wxPython显示正在运行的任务的进度。在

以下是我目前为止的代码:

def OnCellLeftDClick(self, evt):
    if evt.GetCol() == 17:
        dlg = wx.MessageDialog(None, "Do you want to change " + self.GetCellValue(evt.GetRow(), 1) + " bid?",'Updater',wx.YES_NO | wx.ICON_QUESTION)
        result = dlg.ShowModal()
        if result == wx.ID_YES:
            from chanbeBid import changeBidTb
            changeBidTb(self.GetCellValue(evt.GetRow(), 1), self.GetCellValue(evt.GetRow(), 16))

    evt.Skip() 

谢谢你


Tags: 函数进度条用户selfifresult线程yes
1条回答
网友
1楼 · 发布于 2024-10-03 15:27:13

您可以使用\u thread模块创建新线程,并使用pubsub来处理线程之间的通信。一种方法如下所示。我在代码中添加了一些注释。在

import wx
import time
import _thread
from pubsub import pub

seconds = 10

class MyFrame(wx.Frame):
    def __init__(self):
        """
        Just a button to start counting
        """
        super().__init__(None, title='Counting until...')
        self.panel  = wx.Panel(self)
        self.button = wx.Button(self.panel, label='Count', pos=(50, 50))
        self.button.Bind(wx.EVT_BUTTON, self.OnCount)

        ## This will subscribe the window to the message 'Finish counting'.
        ## Thus, everytime the message is broadcast the self.Pass method
        ## will be executed.
        pub.subscribe(self.Pass, 'Finish counting')

    def OnCount(self, event):
        dlg = wx.MessageDialog(None, "Do you want to count?", 
                              style=wx.YES_NO|wx.ICON_QUESTION)
        if dlg.ShowModal() == wx.ID_YES:
            self.count()
        else:
            pass

    def count(self):
        ## Creates and starts a new thread that will execute the self.wait
        ## method. Notice that the thread is started before the ProgressDialog
        ## because the ProgressDialog will keep the main thread busy
        _thread.start_new_thread(self.wait, (seconds,))

        ## ProgressDialog that will keep running until maxV is reached or
        ## until self.keepGoing is set to False
        maxV = 100
        dlg = wx.ProgressDialog("Progress dialog example",
                                     "An informative message",
                                     maximum = maxV,
                                     parent=self,
                                     style = 0
                                     | wx.PD_APP_MODAL
                                    #| wx.PD_CAN_ABORT
                                    #| wx.PD_CAN_SKIP
                                     | wx.PD_ELAPSED_TIME
                                    #| wx.PD_ESTIMATED_TIME
                                    #| wx.PD_REMAINING_TIME
                                    #| wx.PD_AUTO_HIDE
                                     )
        self.keepGoing = True
        count = 0
        while self.keepGoing and count < maxV:
            count += 1
            wx.MilliSleep(250)
            wx.SafeYield()
            (keepGoing, skip) = dlg.Update(count)
        dlg.Destroy() 

    def wait(self, secs):
        ## This is the function that is executed by the new thread
        ## when it finishs the wx.CallAfter method broadcast the message
        ## 'Finish counting' that triggers self.Pass as mentioned above.
        time.sleep(secs)
        wx.CallAfter(pub.sendMessage, 'Finish counting')

    def Pass(self):
        ## Changes self.keepGoing to false so the ProgressDialog is destroyed. 
        self.keepGoing = False

if __name__ == "__main__":
    app = wx.App()
    frame = MyFrame()
    frame.Show()
    app.MainLoop()

关于让进度条在对话框窗口中要求继续。这是可以做到的,但您需要构建自定义对话框窗口,并且可能使用wx.Gauge而不是{}。另外,您很可能希望此自定义窗口是模态的(冻结程序中的所有其他窗口,以便用户必须等待线程中运行的函数完成)。为此,将以下方法添加到自定义对话框类中

^{pr2}$

并以与正常wx.Dialogcustom_dialog.ShowModal()相同的方式显示自定义对话框

相关问题 更多 >