wxpython 按钮控件如何实现运行和中断状态,wxpython:当我单击停止按钮时,是否可以停止正在运行的程序?...

我只是稍微修改了你的代码。我添加了一个线程来运行calculate方法,因此它不会阻塞主线程。在import wx

import time

import threading

class Test(wx.Frame):

def __init__(self, title):

super().__init__(None, title=title)

self.panel = wx.Panel(self)

self.initUI()

self.Centre()

def initUI(self):

vbox = wx.BoxSizer(wx.VERTICAL)

button1 = wx.Button(self.panel, label="START Calculation")

button1.Bind(wx.EVT_BUTTON, self.start_test)

button2 = wx.Button(self.panel, label="STOP Calculation")

button2.Bind(wx.EVT_BUTTON, self.stop_test)

vbox.Add(button1, 0, wx.ALL, 5)

vbox.Add(button2, 0, wx.ALL, 5)

self.panel.SetSizer(vbox)

self.stop = False

def create_thread(self, target):

thread = threading.Thread(target=target)

thread.daemon = True

thread.start()

def calculate(self):

for i in range(20000):

if self.stop:

break

print("Program is running...")

time.sleep(5)

self.stop = False

def start_test(self, e):

self.create_thread(self.calculate)

def stop_test(self, e):

print("The calculation has been stopped!")

self.stop = True

if __name__ == '__main__':

app = wx.App()

Test("A large program").Show()

app.MainLoop()

如果自动停止是真的,它从foor循环中跳出。这样,您就可以开始和停止计算,只需单击两个按钮。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值