Reputation: 77
I want to play with T. When I start, I want to have 2 option stop or pause. when I stop I must restart my counter of 0. but when I pause I must restart and continue where I follow myself stopped.
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()
the stop functionality already works, I need to pause now
Upvotes: 1
Views: 39
Reputation: 211278
You've a state which indicates if the thread is running (self.stop_thread
) and a state which indicates if it is paused. That's all you need. Use the while
loop in the thread and run the thread as long it is not stopped and as ling the counter (i
less than 5. Increment and print the counter in the loop, if the thread is not paused.
When the onButton
event occurs, then the thread is only allowed to be started if the theread is not runinng (self.stop_thread == False
). e.g.:
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()
Upvotes: 1