使用切换按钮
Use ToggleButton
我想和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()
停止功能已经可用,我现在需要暂停
您有一个指示线程是否处于 运行ning (self.stop_thread
) 的状态和一个指示线程是否暂停的状态。这就是你所需要的。在线程中使用 while
循环并在线程中使用 运行 线程,只要它没有停止并作为计数器(i
小于 5。递增并在循环中打印计数器,如果线程没有暂停。
当 onButton
事件发生时,只有当 theread 不是 运行inng (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()
我想和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()
停止功能已经可用,我现在需要暂停
您有一个指示线程是否处于 运行ning (self.stop_thread
) 的状态和一个指示线程是否暂停的状态。这就是你所需要的。在线程中使用 while
循环并在线程中使用 运行 线程,只要它没有停止并作为计数器(i
小于 5。递增并在循环中打印计数器,如果线程没有暂停。
当 onButton
事件发生时,只有当 theread 不是 运行inng (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()