1

我用 wx.slider 和 wx.togglebutton 制作了一个 GUI。我的目标是将滑块用作情节的时间线,将切换按钮用作开始/停止按钮。我的想法是:当我按下切换按钮时,滑块值开始增加,当我再次按下切换按钮时它停止。我正在使用以下代码,我可以使滑块值增加,但是一旦它开始移动,我无法停止循环,直到它到达滑块的末尾。当我再次按下切换按钮时,有没有办法停止增加?

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)

谢谢你的帮助。亲切的问候伊沃。

4

2 回答 2

1

使用计时器:http: //xoomer.virgilio.it/infinity77/wxPython/Widgets/wx.Timer.html

否则,您可以使用 while 循环来评估可以像这样更改的变量:

        while external_variable and i < 100:

        if i == 100:
            self.m_slider1.SetValue(100)
        else:
            self.m_slider1.SetValue(i)
            time.sleep(0.1)
        i += 1
于 2013-08-12T19:19:13.997 回答
1

我写了一个快速脚本,我认为它会做你想做的事:

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()
于 2013-08-12T20:30:43.240 回答