【问题标题】:Wx python update Timer interval using user input使用用户输入的 Wx python 更新定时器间隔
【发布时间】:2020-03-11 00:08:58
【问题描述】:

我似乎找不到使用用户输入动态更新我的 wx 计时器开始间隔的方法。

我正在尝试创建一个可以更新计数时间间隔的计数器程序

下面是我的 Frame 类中的开始时间定义

class MyFrame(wx.Frame):
    def __init__(self,parent,title):
        global interval
        super(MyFrame,self).__init__(parent,title=title,style=wx.MINIMIZE_BOX \
             | wx.SYSTEM_MENU\
             | wx.CAPTION | wx.CLOSE_BOX\
             | wx.CLIP_CHILDREN, size=(1000,1000))
        self.interval=3000
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.OnTimer,self.timer)

        self.timer.Start(self.interval) 

        self.button11 = wx.Button(self, id=wx.ID_ANY, label="Cycle Time",size=(95,60))
        self.button11.Bind(wx.EVT_BUTTON, self.Cycletime)


    def Cycletime(self,e):
        global interval
        self.Pause(e)
        self.t1 = wx.TextEntryDialog(self, 'Enter Cycle time','Cycle Time',"",style=wx.OK | wx.CANCEL)
        self.t1.ShowModal()

        if self.t1.ShowModal()==wx.ID_OK:


            x_i=self.t1.GetValue()
            x_f=float(x_i)
            print(x_i)
            self.interval=x_f

我的类变量永远不会更新,有什么建议吗?

【问题讨论】:

    标签: python-3.x user-interface wxpython wxwidgets


    【解决方案1】:

    您需要停止计时器,然后重新启动它!

    import wx
    
    class MyFrame(wx.Frame):
        def __init__(self,parent,title):
            global interval
            super(MyFrame,self).__init__(parent,title=title,style=wx.MINIMIZE_BOX \
                 | wx.SYSTEM_MENU\
                 | wx.CAPTION | wx.CLOSE_BOX\
                 | wx.CLIP_CHILDREN, size=(200,200))
            self.interval=3000
            self.timer = wx.Timer(self)
            self.Bind(wx.EVT_TIMER, self.OnTimer,self.timer)
    
            self.timer.Start(self.interval)
    
            self.text = wx.StaticText(self, id=wx.ID_ANY, label="Timer",pos=(10,10))
            self.button11 = wx.Button(self, id=wx.ID_ANY, label="Cycle Time",pos=(10,60))
            self.button11.Bind(wx.EVT_BUTTON, self.Cycletime)
            self.counter = 0
            self.Show()
    
        def Cycletime(self,e):
            self.t1 = wx.TextEntryDialog(self, 'Enter Cycle time','Cycle Time',"",style=wx.OK | wx.CANCEL)
            res = self.t1.ShowModal()
            if res == wx.ID_OK:
                x_i=int(self.t1.GetValue())
                self.interval=x_i*1000
                self.timer.Stop()
                self.timer.Start(self.interval)
                print (self.interval)
            self.t1.Destroy()
    
        def OnTimer(self, evt):
            print (self.counter)
            self.counter += 1
    
    if __name__ == '__main__':
        app = wx.App()
        frame = MyFrame(None, "timer")
        app.MainLoop()
    

    【讨论】:

    • 实际上,自从slightly more than 19 years 以来,不再需要在(重新)启动它之前停止计时器。但是你当然需要用新的时间间隔调用Start()
    • @VZ。很高兴知道我是最新的并且在球上 :-) 显然,这让“老式”有点远,即使对我来说也是如此!
    • 非常感谢!这么简单的答案!!
    猜你喜欢
    • 2012-05-18
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多