使用ToggleButton

问题描述 投票:1回答:1

我想和T一起玩。开始时,我想有2个选项停止或暂停。当我停止时,我必须重新启动计数器0。但是,当我暂停时,我必须重新启动并继续跟随自己停止的位置。

import time
import sys

class ToggleButtonDemo(wx.Frame):
    def __init__(self, parent):
        super().__init__(parent)
        self.panel = wx.Panel(self, -1)
        self.startbutton = wx.ToggleButton(self.panel, -1, "Start")
        self.stopbutton = wx.Button(self.panel, -1, "stop")
        self.stopbutton.Disable()

        self.startbutton.Bind(wx.EVT_TOGGLEBUTTON, self.onButton)
        self.stopbutton.Bind(wx.EVT_BUTTON, self.onStop)
        vbox = wx.BoxSizer(wx.VERTICAL)
        vbox.Add(self.startbutton)
        vbox.Add(self.stopbutton)
        #self.keepGoing = False
        self.panel.SetSizer(vbox)
        self.Show()
        self.etat = True
        self.stop_thread = False        
    def activity(self):
        while self.stop_thread == True:
            for i in range(5):
                print (i)
                time.sleep(1)
                if self.stop_thread == False:
                    #stop
                    return self.stop_thread
                if self.etat == False:
                    print("Pause")
                    break
            self.startbutton.SetLabel("start")
            self.stopbutton.SetLabel("stop")
            self.stopbutton.Disable()
            if self.etat == True:
                self.startbutton.SetValue(False)
            else:
                self.startbutton.SetValue(True)
            return self.stop_thread
    def onButton(self, event):
        self.etat = self.startbutton.GetValue()
        if self.etat == True:
            self.stop_thread = True
            import threading
            self.t = threading.Thread(target = self.activity)
            self.t.start()
            event.GetEventObject().SetLabel("Pause")
            self.stopbutton.Enable()
        if self.etat == False:
            self.etat = False
            #Pause code
            event.GetEventObject().SetLabel("Start")
            self.stopbutton.Disable()
    def onStop(self, event):
        self.stop_thread = False
        self.startbutton.SetLabel("Start")
        self.stopbutton.Disable()
        self.startbutton.SetValue(False)
app = wx.App()
prog = ToggleButtonDemo(None)
app.MainLoop()

停止功能已经起作用,我需要立即暂停

python wxpython python-3.5
1个回答
1
投票

您有一个状态,该状态指示线程是否正在运行(self.stop_thread),以及一个状态,指示线程是否已暂停。这就是您所需要的。在线程中使用while循环并运行该线程,只要它没有停止并运行计数器(i小于5)即可。如果线程未暂停,则在循环中递增并打印计数器。当发生onButton事件时,仅在读取未运行(self.stop_thread == False)时才允许启动线程。例如:

class ToggleButtonDemo(wx.Frame):
    # [...]

    def activity(self):
        # run thread as long not stopped and i <  max_i 
        max_i = 5
        i = 0
        while self.stop_thread and i < max_i:
            if self.etat:
                print(i)
                i = i+1
            time.sleep(1)
        # ensure that stop state is not set (so thread can start again)
        self.stop_thread = False
        self.etat = True
        self.startbutton.SetLabel("Start")
        self.stopbutton.SetLabel("Stop")
        self.stopbutton.Disable()
        self.startbutton.SetValue(False)
        return self.stop_thread

    def onButton(self, event):
        self.etat = self.startbutton.GetValue()
        # start thread in not running
        if self.etat == True:
            if self.stop_thread == False:
                self.stop_thread = True
                self.t = threading.Thread(target = self.activity)
                self.t.start()
                event.GetEventObject().SetLabel("Pause")
            self.stopbutton.Enable()      
        # pause
        if self.etat == False:
            self.etat = False
            event.GetEventObject().SetLabel("Start")
            self.stopbutton.Disable()
© www.soinside.com 2019 - 2024. All rights reserved.