Search code examples
pythonwxpythontogglebutton

How can I stop a loop with a Toggle Button


I´ve made a GUI with a wx.slider and a wx.togglebutton. My goal is to use the slider as a kind of a timeline for a plot and the toggle button as a start/stop button. My ideia is: when I press the toggle button the slider value starts increasing and when I press the toggle button again it stops. I'm using the following code and I can make the slider value increase but once it starts moving I can't stop the loop until it reaches the end of the slider. Is there a way to stop the increase when I press the toggle button again?

def m_toggleBtn1OnToggleButton( self, event ):
    value = self.m_toggleBtn1.GetValue()
    if value == True:
        self.m_toggleBtn1.SetLabel("Pause")
        for i in xrange(100):
            if i == 100:
                self.m_slider1.SetValue(100)
            else:
                self.m_slider1.SetValue(i)
                time.sleep(0.1)
    else:
        self.m_toggleBtn1.SetLabel("Start")
        slider_value = self.m_slider1.GetValue()
        self.m_slider1.SetValue(slider_value)

Thanks for your help. Kind Regards Ivo.


Solution

  • I wrote up a quick script that I think will do what you want:

    import wx
    
    ########################################################################
    class MyPanel(wx.Panel):
        """"""
    
        #----------------------------------------------------------------------
        def __init__(self, parent):
            """Constructor"""
            wx.Panel.__init__(self, parent)
            self.slider_value = 1
    
            self.slider = wx.Slider(self, value=1, minValue=0, maxValue=100, size=(250, -1),
                                    style=wx.SL_HORIZONTAL | wx.SL_AUTOTICKS | wx.SL_LABELS)
            self.slider.SetTickFreq(5, 1)
            self.slider.Bind(wx.EVT_SCROLL, self.onScroll)
            self.toggle = wx.ToggleButton(self, label="Start")
            self.toggle.Bind(wx.EVT_TOGGLEBUTTON, self.onToggle)
    
            self.timer = wx.Timer(self)
            self.Bind(wx.EVT_TIMER, self.onUpdate, self.timer)
    
            sizer = wx.BoxSizer(wx.VERTICAL)
            sizer.Add(self.slider, 0, wx.ALL, 5)
            sizer.Add(self.toggle, 0, wx.ALL, 5)
            self.SetSizer(sizer)
    
        #----------------------------------------------------------------------
        def onScroll(self, event):
            """
            Fires when you manually scroll the slider
            """
            value = self.slider.GetValue()
            self.slider_value = value
    
        #----------------------------------------------------------------------
        def onToggle(self, event):
            """
            Start / Stop the slider incrementing
            """
            value = self.toggle.GetValue()
            if value:
                self.toggle.SetLabel("Pause")
                self.timer.Start(1000)
            else:
                self.timer.Stop()
                self.toggle.SetLabel("Start")
    
        #----------------------------------------------------------------------
        def onUpdate(self, event):
            """
            Increment the slider
            """
            self.slider_value += 1
            self.slider.SetValue(self.slider_value)
    
    ########################################################################
    class MyFrame(wx.Frame):
        """"""
    
        #----------------------------------------------------------------------
        def __init__(self):
            """Constructor"""
            wx.Frame.__init__(self, None, title="Slider Test")
            panel = MyPanel(self)
            self.Show()
    
    if __name__ == "__main__":
        app = wx.App(False)
        frame = MyFrame()
        app.MainLoop()