Wxpython GUI中的循环

Wxpython GUI中的循环,wxpython,python-multithreading,Wxpython,Python Multithreading,我在wxPython中有一个面板,我想在其中选择一个按钮启用监视器,单击后启动下面的循环,但也再次释放GUI,从而将按钮标签更新为禁用监视器。单击disable后,它将完全停止循环 我已经研究了线程,但我不确定在这种情况下我应该做什么 整个循环在defstartstop(self)声明中运行,并在wxPanel的类中运行 我有点不知所措,但我已经对这个GUI进行了一段时间的修补,我很想用正确的方式完成这节课。:) 伪代码: while zonesToMonitor != []:

我在wxPython中有一个面板,我想在其中选择一个按钮
启用监视器
,单击后启动下面的循环,但也再次释放GUI,从而将按钮标签更新为
禁用监视器
。单击
disable
后,它将完全停止循环

我已经研究了
线程
,但我不确定在这种情况下我应该做什么

整个循环在
defstartstop(self)
声明中运行,并在wxPanel的
类中运行

我有点不知所措,但我已经对这个GUI进行了一段时间的修补,我很想用正确的方式完成这节课。:)

伪代码:

 while zonesToMonitor != []: 
        time.sleep(int(self.tc_CheckInterval.Value))

        j = 0
        for i in zonesToMonitor:
            maxVOL = maxVolPerZone[j]

            urllib.urlopen("http://" + ip_address + ":" + self.tc_serverPort.Value +"/data/rendererData?data=R::" + wx.FindWindowByLabel(i).Label).read()

            INFO = urllib.urlopen("http://" + ip_address + ":" + self.tc_serverPort.Value +"/data/rendererAction?data=class").read()

            curTime = datetime.datetime.now()
            curTime = curTime.strftime("%H:%M")

            if self.ck_QuietHours.Value == True:
                quietStartHr = self.tc_quietHr.Value
                quietEndHr = self.tc_quietHrStop.Value
                quietVol = self.tc_QuietVol.Value
                if (curTime > quietStartHr) and (curTime < quietEndHr):
                    print "In quiet hours..."
                    maxVOL = quietVol            

            if self.ck_MuteHours.Value == True:
                muteStartHr = self.tc_MuteHr.Value
                muteEndHr = self.tc_MuteHrStop.Value                   
                if (curTime > muteStartHr) and (curTime < muteEndHr):
                    print "In mute time..."
                    maxVOL = 0

            OUTPUT = re.findall('(?<=VOLUME::).*?(?=_\|_)', INFO)

            if maxVOL == '':
                maxVOL = 0

            if OUTPUT == '':
                OUTPUT = 0

            OUTPUT = map(int, OUTPUT)             

            if OUTPUT > int(maxVOL):
                url = "http://" + ip_address + ":" + self.tc_serverPort.Value + "/data/rendererAction?data=VOLUME::" + str(maxVOL)
                urllib.urlopen(url).read()

            j += 1
while zonesToMonitor!=[]: 
睡眠时间(int(self.tc\u CheckInterval.Value))
j=0
对于zonesToMonitor中的i:
maxVOL=maxVolPerZone[j]
urllib.urlopen(“http://“+ip_address+”:“+self.tc_serverPort.Value+”/data/rendererData?data=R::”+wx.FindWindowByLabel(i.Label).read()
INFO=urllib.urlopen(“http://“+ip_address+”:“+self.tc_serverPort.Value+”/data/renderAction?data=class”).read()
curTime=datetime.datetime.now()
curTime=curTime.strftime(“%H:%M”)
如果self.ck_QuietHours.Value==True:
quietStartHr=self.tc\u quietHr.Value
quietEndHr=self.tc\u quietHrStop.Value
quietVol=self.tc\u quietVol.Value
如果(curTime>quietStartHr)和(curTimemuteStartHr)和(curTimeOUTPUT=re.findall(')(?我认为用线程实现这种任务并不是一个坏的选择,您可以在这里阅读关于线程和wxpython的更多信息:,也可以在这里阅读:作者在这里讨论了用wxpython执行线程的替代方案

可通过以下方式完成:

class MyThread(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.ToKill = False
    def run(self):
        while True:
            self.FooHandler()
            if self.ToKill:
                return None
    def FooHandler(self):
        """ your function here """
        print 3

class Panel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, id=-1, style=wx.RAISED_BORDER)
        Bsizer = wx.BoxSizer(wx.VERTICAL)
        button=wx.ToggleButton(self, label="Click To Enable")

        Bsizer.Add(button,1,wx.ALL | wx.EXPAND)
        self.SetSizer(Bsizer)
        self.Bind(wx.EVT_TOGGLEBUTTON,self.buttonEvt,id=button.GetId())

    def buttonEvt(self, evt):
        clickedToggleButton = evt.GetEventObject()
        if clickedToggleButton.GetValue():
            self.thread = MyThread()
            self.thread.start()
            clickedToggleButton.SetLabel("Click To Disable")
        else:
            self.thread.ToKill = True
            clickedToggleButton.SetLabel("Click To Enable")

我并不认为用线程实现这种任务是一个坏的选择,你可以在这里阅读更多关于线程和wxpython的内容,也可以在这里阅读:作者在这里讨论了用wxpython实现线程的替代方案

可通过以下方式完成:

class MyThread(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.ToKill = False
    def run(self):
        while True:
            self.FooHandler()
            if self.ToKill:
                return None
    def FooHandler(self):
        """ your function here """
        print 3

class Panel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, id=-1, style=wx.RAISED_BORDER)
        Bsizer = wx.BoxSizer(wx.VERTICAL)
        button=wx.ToggleButton(self, label="Click To Enable")

        Bsizer.Add(button,1,wx.ALL | wx.EXPAND)
        self.SetSizer(Bsizer)
        self.Bind(wx.EVT_TOGGLEBUTTON,self.buttonEvt,id=button.GetId())

    def buttonEvt(self, evt):
        clickedToggleButton = evt.GetEventObject()
        if clickedToggleButton.GetValue():
            self.thread = MyThread()
            self.thread.start()
            clickedToggleButton.SetLabel("Click To Disable")
        else:
            self.thread.ToKill = True
            clickedToggleButton.SetLabel("Click To Enable")
哇——谢谢你——我终于,终于,在从互联网上撕下无数个小时的示例代码后,明白了如何使这项工作正常进行。谢谢你让它保持简单。哇——谢谢你——我终于,终于,明白了在从互联网上撕下无数个小时的示例代码后,如何使这项工作正常进行。谢谢你让它保持简单。:)