首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用ToggleButton

使用ToggleButton
EN

Stack Overflow用户
提问于 2019-09-26 13:58:40
回答 1查看 29关注 0票数 1

我想玩T,当我开始,我想有两个选项停止或暂停。当我停止时,我必须重新启动我的计数器0。但是当我停下来的时候,我必须重新开始,继续我跟随自己停止的地方。

代码语言:javascript
运行
复制
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()

停止功能已经工作,我现在需要暂停。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-26 15:31:16

您有一个指示线程是否正在运行的状态(self.stop_thread)和一个指示是否暂停的状态。这就是你所需要的。在线程中使用while循环,并运行该线程,只要它不停止,并作为对计数器(i小于5。增量和打印计数器在循环中,如果线程没有暂停。

当发生onButton事件时,只有当线程没有运行(self.stop_thread == False)时,才允许启动线程。例如:

代码语言:javascript
运行
复制
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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58118488

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档