python-wx.lib.pubsub:如何更改计时器中的值
作者:互联网
您好,我有一个问题,计时器设置为1000毫秒,我想用单选按钮更改该值.上面有一个示例代码,但我不知道确切的操作方法.wx.lib.pubsub是否可能更改计时器的值?
有人可以给我一个例子吗?
这是代码:
import wx
import time
class SettingsFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY,size=(100,200))
self.CenterOnParent()
self.SetBackgroundColour('#e4e4e4')
self.radio1 = wx.RadioButton(self, label="1 sec",pos=(40,45))
self.Bind(wx.EVT_RADIOBUTTON, self.SetLab1)
self.radio2 = wx.RadioButton(self, label="5 sec",pos=(40,65))
self.Bind(wx.EVT_RADIOBUTTON, self.SetLab2)
self.radio3 = wx.RadioButton(self, label="10 sec",pos=(40,85))
self.Bind(wx.EVT_RADIOBUTTON, self.SetLab3)
extBtn = wx.Button(self, label="Exit",pos=(20,110))
extBtn.Bind(wx.EVT_BUTTON, self.extFrame)
def SetLab1(self,event):
self.Show()
def SetLab2(self,event):
self.Show()
def SetLab3(self,event):
self.Show
def extFrame(self,event):
self.Close()
class MainPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent)
self.frame = parent
self.CenterOnParent()
setBtn = wx.Button(self, label="Set",pos=(45,10))
setBtn.Bind(wx.EVT_BUTTON, self.setFrame)
self.redraw_timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.on_redraw_timer, self.redraw_timer)
self.redraw_timer.Start(milliseconds=1000)
def setFrame(self, event):
SettingsFrame().Show()
def on_redraw_timer(self, event):
print "Test: "+time.ctime()
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY,size=(200,200))
panel = MainPanel(self)
self.CenterOnParent()
if __name__ == "__main__":
app = wx.PySimpleApp()
frame = MainFrame()
frame.Show()
app.MainLoop()
解决方法:
非pubsub版本
将更改计时器方法添加到MainPanel中,以更改计时器的起始值
创建SettingsFrame时,将MainPanel实例的引用传递给它.
将引用存储到SettingsFrame上的父对象.
正确的RadioButton 1从
self.radio2
至
self.radio1.
更改绑定,使它们像这样绑定到单选按钮本身
self.radio1.Bind(wx.EVT_RADIOBUTTON, self.SetLab1)
在事件句柄中,使用新值调用父更改计时器方法
self.parent.change_timer(1000)
这是完整的代码,以防万一我错过了更改的步骤之一
import wx
import time
class SettingsFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, None, wx.ID_ANY, size=(100, 200))
self.parent = parent
self.CenterOnParent()
self.SetBackgroundColour('#e4e4e4')
self.radio1 = wx.RadioButton(self, label="1 sec", pos=(40, 45))
self.radio1.Bind(wx.EVT_RADIOBUTTON, self.SetLab1)
self.radio2 = wx.RadioButton(self, label="5 sec", pos=(40, 65))
self.radio2.Bind(wx.EVT_RADIOBUTTON, self.SetLab2)
self.radio3 = wx.RadioButton(self, label="10 sec", pos=(40, 85))
self.radio3.Bind(wx.EVT_RADIOBUTTON, self.SetLab3)
extBtn = wx.Button(self, label="Exit", pos=(20, 110))
extBtn.Bind(wx.EVT_BUTTON, self.extFrame)
def SetLab1(self, event):
self.parent.change_timer(1000)
def SetLab2(self, event):
self.parent.change_timer(5000)
def SetLab3(self, event):
self.parent.change_timer(10000)
def extFrame(self, event):
self.Close()
class MainPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent)
self.frame = parent
self.CenterOnParent()
setBtn = wx.Button(self, label="Set", pos=(45, 10))
setBtn.Bind(wx.EVT_BUTTON, self.setFrame)
self.redraw_timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.on_redraw_timer, self.redraw_timer)
self.redraw_timer.Start(milliseconds=1000)
def setFrame(self, event):
SettingsFrame(self).Show()
def on_redraw_timer(self, event):
print "Test: " + time.ctime()
def change_timer(self, value):
print 'Changing timer to:', value
self.redraw_timer.Start(value)
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, size=(200, 200))
panel = MainPanel(self)
self.CenterOnParent()
if __name__ == "__main__":
app = wx.App(False)
frame = MainFrame()
frame.Show()
app.MainLoop()
使用定制事件
这是使用从SettingsPanel发送的,可以绑定到MainPanel的自定义事件来完成此操作的另一种方法.
import time
import wx
from wx.lib.newevent import NewCommandEvent
TimerChangeEvent, EVT_TIMER_CHANGE = NewCommandEvent()
class SettingsFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, size=(100, 200))
self.CenterOnParent()
self.SetBackgroundColour('#e4e4e4')
self.radio1 = wx.RadioButton(self, label="1 sec", pos=(40, 45))
self.radio1.Bind(wx.EVT_RADIOBUTTON, self.SetLab1)
self.radio2 = wx.RadioButton(self, label="5 sec", pos=(40, 65))
self.radio2.Bind(wx.EVT_RADIOBUTTON, self.SetLab2)
self.radio3 = wx.RadioButton(self, label="10 sec", pos=(40, 85))
self.radio3.Bind(wx.EVT_RADIOBUTTON, self.SetLab3)
extBtn = wx.Button(self, label="Exit", pos=(20, 110))
extBtn.Bind(wx.EVT_BUTTON, self.extFrame)
def SetLab1(self, event):
self.post_timer_change_event(1000)
def SetLab2(self, event):
self.post_timer_change_event(5000)
def SetLab3(self, event):
self.post_timer_change_event(10000)
def post_timer_change_event(self, value):
evt = TimerChangeEvent(self.Id, value=value)
wx.PostEvent(self, evt)
def extFrame(self, event):
self.Close()
class MainPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent)
self.frame = parent
self.CenterOnParent()
setBtn = wx.Button(self, label="Set", pos=(45, 10))
setBtn.Bind(wx.EVT_BUTTON, self.setFrame)
self.redraw_timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.on_redraw_timer, self.redraw_timer)
self.redraw_timer.Start(milliseconds=1000)
def setFrame(self, event):
settings_frame = SettingsFrame()
settings_frame.Show()
settings_frame.Bind(EVT_TIMER_CHANGE, self.on_change_timer)
def on_redraw_timer(self, event):
print "Test: " + time.ctime()
def on_change_timer(self, event):
value = event.value
print 'Changing timer to:', value
self.redraw_timer.Start(value)
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, size=(200, 200))
panel = MainPanel(self)
self.CenterOnParent()
if __name__ == "__main__":
app = wx.App(False)
frame = MainFrame()
frame.Show()
app.MainLoop()
标签:publish-subscribe,timer,wxpython,python 来源: https://codeday.me/bug/20191122/2058115.html