多重for循环终止

点击开始,开始循环,点击停止计数结束。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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值