如何暂停执行功能

时间:2019-09-17 15:45:03

标签: python-3.x wxpython phoenix

我想玩3个按钮。我必须暂停执行函数,然后在停止(取消暂停)的地方重新开始执行。我还必须停止执行函数,并从头开始重新执行。 取消按钮还必须像停止一样停止执行。按下按钮(任何按钮)后,PyprogressDialog必须消失。谢谢

import wx
import time
class PyProgressDemo(wx.Frame):
    def __init__(self, parent):
        super().__init__(parent)

        self.panel = wx.Panel(self, -1)

        self.startbutton = wx.Button(self.panel, -1, "Start with PyProgress!")
        self.pausebutton = wx.Button(self.panel, -1, "Pause/Unpause PyProgress!")
        self.stopbutton = wx.Button(self.panel, -1, "stop all thing")

        self.startbutton.Bind(wx.EVT_BUTTON, self.onButton)
        self.pausebutton.Bind(wx.EVT_BUTTON, self.onPause)
        self.stopbutton.Bind(wx.EVT_BUTTON, self.onStop)

        vbox = wx.BoxSizer(wx.VERTICAL)
        vbox.Add(self.startbutton)
        vbox.Add(self.pausebutton)
        vbox.Add(self.stopbutton)

        self.panel.SetSizer(vbox)
        self.Show()

        import threading
        self.shutdown_event = threading.Event()

    def activity(self):
        while not self.shutdown_event.is_set():
            for i in range(10):
                print (i)
                time.sleep(1)
                if self.shutdown_event.is_set():
                    break

            print("stop")
            self.keepGoing = True
        self.shutdown_event.set()

    def onButton(self, event):
        import threading
        threading.Thread(target = self.activity).start()

        self.dlg = wx.ProgressDialog('title', 'Some thing in progresse...', 
                                     style= wx.PD_ELAPSED_TIME 
                                     | wx.PD_CAN_ABORT)      

        self.keepGoing = False
        while self.keepGoing == False:
            wx.MilliSleep(30)
            keepGoing = self.dlg.Pulse()
        self.dlg.Destroy()

    def onPause(self, event):
        #pause/unpause
        pass

    def onStop(self, event):
        #and wx.PD_CAN_ABORT
        self.shutdown_event.set()

app = wx.App()
prog = PyProgressDemo(None)
app.MainLoop()

1 个答案:

答案 0 :(得分:0)

这是一个简单的示例,单击该按钮将打开进度对话框并启动模拟任务。单击取消按钮后,进度对话框将关闭,长时间运行的任务将停止。单击恢复按钮后,它将重新打开进度对话框并重新启动长时间运行的任务。

import wx, traceback


class Mainframe(wx.Frame):
    def __init__(self):
        super().__init__(None)
        self.btn = wx.Button(self, label="Start")
        self.btn.Bind(wx.EVT_BUTTON, self.on_btn)
        # progress tracks the completion percent
        self._progress = 0
        self._task_running = False
        self.dialog = None  # type: wx.ProgressDialog
        self.CenterOnScreen(wx.BOTH)
        self.Show()
        self.poll()

    def on_btn(self, evt):
        # is there a dialog already opened?
        if not self.dialog:
            # create a progress dialog with a cancel button
            self.dialog = wx.ProgressDialog("title", "message", 100, self, wx.PD_CAN_ABORT)
            self.dialog.Show()
            self.btn.SetLabel("Running")
            self.start_long_running_task()

    def start_long_running_task(self):
        if not self._task_running:
            print("starting long running task")
            self._task_running = True
            self.long_running_task()

    def long_running_task(self):
        """ this method simulates a long-running task,
        normally it would be run in a separate thread so as not to block the UI"""
        if not self:
            return  # the frame was closed

        if self.dialog:
            # increment the progress percent
            self._progress += 1
            if self._progress > 100:
                self._progress = 0
            wx.CallLater(300, self.long_running_task)
        else:
            # the progress dialog was closed
            print("task stopped")
            self._task_running = False

    def poll(self):
        """ this method runs every 0.3 seconds to update the progress dialog, in an actual implementation
         self._progress would be updated by the method doing the long-running task"""

        if not self:
            return  # the frame was closed
        if self.dialog:
            # the cancel button was pressed, close the dialog and set the button label to 'Resume'
            if self.dialog.WasCancelled():
                self.btn.SetLabel("Resume")
                self.dialog.Destroy()
            else:
                # update the progress dialog with the current percentage
                self.dialog.Update(self._progress, "%s%% complete" % self._progress)
        wx.CallLater(300, self.poll)


try:
    app = wx.App()
    frame = Mainframe()
    app.MainLoop()
except:
    input(traceback.format_exc())