Wx python 使用用户输入更新定时器间隔

Wx python update Timer interval using user input

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

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

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

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

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

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

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()