点击开始,开始循环,点击停止计数结束。for循环嵌套,或while与dor循环嵌套
wxpython
import time
import wx
import threading
import random
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "多线程计数器")
self.panel = wx.Panel(self, pos=(100, 100), size=(500, 500))
self.is_running = False
self.start_button = wx.Button(self.panel, label="开始计数",pos=(50, 20), size=(100, 30))
self.stop_button = wx.Button(self.panel, label="停止计数",pos=(50, 50), size=(100, 30))
self.count_label = wx.TextCtrl(self.panel,pos=(50, 90), size=(100, 30))
self.stop_button1 = wx.Button(self.panel, label="按钮", pos=(200, 50), size=(100, 30))
self.stop_button1.Bind(wx.EVT_BUTTON,self.set)
self.count_label1 = wx.TextCtrl(self.panel , pos=(200, 90), size=(100, 30))
self.start_button.Bind(wx.EVT_BUTTON, self.start_count)
self.stop_button.Bind(wx.EVT_BUTTON, self.stop_count)
def start_count(self, event):
if not self.is_running:
self.is_running = True
print(self.is_running)
print("开始运行")
self.count_thread = threading.Thread(target=self.counting)
self.count_thread.start()
def stop_count(self, event):
self.is_running = False
print("结束按钮")
def counting(self):
while self.is_running:
for x in range(0, 3) :
for i in range(1, 5) :
count = i
print(count)
self.count_label.SetValue(str(count))
print("is_running为: {}".format(self.is_running))
time.sleep(3)
if not self.is_running :
break
else:
continue
print("第{}遍结束".format(x + 1))
if not self.is_running :
break
else :
continue
break
def set(self,event):
hah = random.randint(1,50)
hah = str(hah)
self.count_label1.SetValue("文字{}".format(hah))
if __name__ == '__main__':
app = wx.App()
frame = MyFrame()
frame.Show()
app.MainLoop()