Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何暂停函数的执行_Python 3.x_Wxpython_Phoenix - Fatal编程技术网

Python 3.x 如何暂停函数的执行

Python 3.x 如何暂停函数的执行,python-3.x,wxpython,phoenix,Python 3.x,Wxpython,Phoenix,我想玩3个按钮。我必须暂停函数的执行,并在停止的位置重新开始执行(取消暂停)。我还必须停止函数的执行,并从一开始就重新开始执行。 取消按钮还必须停止执行,如停止。PyprogressDialog必须在按下按钮(任何按钮)后消失。谢谢 import wx import time class PyProgressDemo(wx.Frame): def __init__(self, parent): super().__init__(parent) self.

我想玩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()


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

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())

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

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())

有关暂停进度对话框的想法,请参见本答案中的第二个代码示例。有关暂停进度对话框的想法,请参见本答案中的第二个代码示例。谢谢。但是我不知道您使用的是哪种
版本的python
。使用
Python3.5.2
时,这一行有一个synthax错误:
self.dialog.Update(self.\u progress,f“{self.\u progress}%complete”)
@LandryEngolo在Python3.6中引入了字符串,我对其进行了更新,使其与Python3.5兼容。谢谢。但是我不知道您使用的是哪种
版本的python
。使用
Python3.5.2
时,这一行有一个synthax错误:
self.dialog.Update(self.\u progress,f“{self.\u progress}%complete”)
在Python3.6中引入了@LandryEngolo f字符串,我对其进行了更新,使其与Python3.5兼容