python - How can I stop a loop with a Toggle Button -
i´ve made gui wx.slider , wx.togglebutton. goal use slider kind of timeline plot , toggle button start/stop button. ideia is: when press toggle button slider value starts increasing , when press toggle button again stops. i'm using following code , can make slider value increase once starts moving can't stop loop until reaches end of slider. there way stop increase when press toggle button again?
def m_togglebtn1ontogglebutton( self, event ): value = self.m_togglebtn1.getvalue() if value == true: self.m_togglebtn1.setlabel("pause") in xrange(100): if == 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 help. kind regards ivo.
i wrote quick script think 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 manually scroll slider """ value = self.slider.getvalue() self.slider_value = value #---------------------------------------------------------------------- def ontoggle(self, event): """ start / stop 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 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()
Comments
Post a Comment