【发布时间】:2019-05-22 12:12:49
【问题描述】:
假设我有一个需要一段时间才能完成的大型程序,当程序启动时,我发现某些参数设置不正确,所以我想停止它。我不想完全终止程序,而是希望停止运行计算,但仍然显示 wx.Frame。可能吗?下面是示例代码:
import wx
import time
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)
def start_test(self, e):
for i in range(20000):
print("Program is running...")
time.sleep(5)
def stop_test(self, e):
print("How do I stop the test when click this button?")
if __name__ == '__main__':
app = wx.App()
Test("A large program").Show()
app.MainLoop()
如程序所示,当我点击开始按钮时,程序运行。我希望我可以停止程序但仍然保留应用程序窗口。 现在,当我单击开始按钮时,停止按钮甚至无法单击。有没有可能达到我想要的目标?
【问题讨论】:
标签: python python-3.x user-interface wxpython wxwidgets