weixin_53911587 2023-02-24 23:49 采纳率: 100%
浏览 19
已结题

wxpython类中不能停止while循环

创建了一个wx.Frame,面板有两个按钮,点击"start"按钮后文本不断显示随机数,可是点击“stop”按钮后却不能停止循环,不知道代码哪里出问题,请各位指点,感谢!

import wx
import random
import time
n=0
b=False
class MyFrame(wx.Frame):
    def __init__(self):
        super().__init__(None,title='random',size=(300,300),pos=(100,100))
        panel=wx.Panel(parent=self)
        self.st1=wx.StaticText(parent=panel,label='0',pos=(100,50))
        self.st2=wx.StaticText(parent=panel,label='0',pos=(130,50))
        self.b1=wx.Button(parent=panel,id=1,label='start',pos=(50,100))
        self.b2=wx.Button(parent=panel,id=2,label='stop',pos=(150,100))
        #self.Bind(wx.EVT_BUTTON,self.on_click,id=1,id2=10)
        self.b1.Bind(wx.EVT_BUTTON,self.startBtn)
        self.b2.Bind(wx.EVT_BUTTON,self.stopBtn)


    def stopBtn(self,event):
        b=False
        
    def startBtn(self,event):
        b=True
        global n
        while b:
            n=random.randint(1,10)
            self.st1.SetLabelText(str(n))
            time.sleep(0.5)



if __name__=="__main__":
    app=wx.App()
    frm=MyFrame()
    frm.Show()
    app.MainLoop()
            

  • 写回答

2条回答 默认 最新

  • CodeBytes 2023-02-25 00:34
    关注

    该回答引用ChatGPT

    在你的代码中,stopBtn()方法中的变量b只是一个局部变量,并不是startBtn()中定义的全局变量b。因此,当你在stopBtn()中修改b的值时,并不会影响到startBtn()中的b变量。因此,即使你在stopBtn()中将b设置为False,startBtn()方法中的while循环仍然会继续执行。

    为了解决这个问题,你可以在MyFrame类中定义一个成员变量,来保存循环是否应该继续的状态。然后,在startBtn()和stopBtn()方法中分别修改这个成员变量。具体来说,你可以将你的代码修改为如下形式:

    
    import wx
    import random
    import time
    
    
    class MyFrame(wx.Frame):
        def __init__(self):
            super().__init__(None, title='random', size=(300, 300), pos=(100, 100))
            self.panel = wx.Panel(parent=self)
            self.st1 = wx.StaticText(parent=self.panel, label='0', pos=(100, 50))
            self.st2 = wx.StaticText(parent=self.panel, label='0', pos=(130, 50))
            self.b1 = wx.Button(parent=self.panel, id=1, label='start', pos=(50, 100))
            self.b2 = wx.Button(parent=self.panel, id=2, label='stop', pos=(150, 100))
            self.is_running = False
            self.b1.Bind(wx.EVT_BUTTON, self.startBtn)
            self.b2.Bind(wx.EVT_BUTTON, self.stopBtn)
    
        def stopBtn(self, event):
            self.is_running = False
    
        def startBtn(self, event):
            self.is_running = True
            while self.is_running:
                n = random.randint(1, 10)
                self.st1.SetLabelText(str(n))
                time.sleep(0.5)
    
    
    if __name__ == "__main__":
        app = wx.App()
        frm = MyFrame()
        frm.Show()
        app.MainLoop()
    
    

    在修改后的代码中,我们在MyFrame类的__init__()方法中添加了一个成员变量self.is_running,用于表示当前循环是否应该继续。在startBtn()方法中,我们将这个成员变量设置为True,并在while循环中判断它的值。在stopBtn()方法中,我们将这个成员变量设置为False,从而停止循环。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
    1人已打赏
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月7日
  • 已采纳回答 2月27日
  • 创建了问题 2月24日

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改