python - wx.lib.pubsub : How to change the value in a timer

标签 python timer wxpython publish-subscribe

你好,我有一个问题,计时器设置为 1000 毫秒,我想用单选按钮更改该值。上面我有一个示例代码,但我不知 Prop 体怎么做。 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()

最佳答案

您的代码有很多问题。前两个单选按钮绑定(bind)到同一个处理程序,尽管很明显它们不应该绑定(bind)。 SetLab3 没有正确调用 self.Show,但是话又说回来,在任何这些方法中根本调用 self.Show 是没有意义的. wx.PySimpleApp 构造已弃用。你现在应该使用 wx.App 了。

现在实际回答问题本身。是的,您可以使用 pubsub 将信息从 SettingFrame 传回 MainPanel。以下是在 wx 2.9/更新的 pubsub 中的操作方法:

import wx
import time

from wx.lib.pubsub import pub

class SettingsFrame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY,size=(100,200))
        self.CenterOnParent()
        self.SetBackgroundColour('#e4e4e4')

        self.radio2 = wx.RadioButton(self, label="1 sec",pos=(40,45))
        self.Bind(wx.EVT_RADIOBUTTON, self.update_timer)

        self.radio2 = wx.RadioButton(self, label="5 sec",pos=(40,65))
        self.Bind(wx.EVT_RADIOBUTTON, self.update_timer)

        self.radio3 = wx.RadioButton(self, label="10 sec",pos=(40,85))
        self.Bind(wx.EVT_RADIOBUTTON, self.update_timer)

        extBtn = wx.Button(self, label="Exit",pos=(20,110))
        extBtn.Bind(wx.EVT_BUTTON, self.extFrame)

        self.choice = 1

    #----------------------------------------------------------------------
    def update_timer(self, event):
        """"""
        ctrl = event.GetEventObject()
        if ctrl.GetLabel() == "1 sec":
            self.choice = 1000
        elif ctrl.GetLabel() == "5 sec":
            self.choice = 5000
        elif ctrl.GetLabel() == "10 sec":
            self.choice = 10000

    def extFrame(self,event):
        pub.sendMessage("update_timer", message=self.choice)
        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)

        pub.subscribe(self.listener, "update_timer")

    def setFrame(self, event):
        SettingsFrame().Show()

    def on_redraw_timer(self, event):
        print "Test: "+time.ctime()

    def listener(self, message, arg2=None):
        self.redraw_timer.Stop()
        self.redraw_timer.Start(message)

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

请注意,此示例也稍微清理了您的代码。您可以在此处阅读有关 pubsub 的更多信息:

您可能还会发现这个计时器教程很有帮助:

关于python - wx.lib.pubsub : How to change the value in a timer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21186691/

相关文章:

python - 如何使用 Python 和 Gracenote 识别音乐样本?

python - 你如何终止/中断/中止 Python 控制台/sys.stdin readline()?

python - 屏蔽营业时间以外的时间序列

javascript - 计时器上的变色文本? (HTML)

python - 如何使用 Python 创建 Mac OS X 应用程序?

python - 如何使用 wxpython 创建半透明/alpha 透明矩形?

python - wxPython 的 "Inception"情况

python - 使用 numpy 提高组合 for block 的性能

timer - Arduino 纳米定时器

javascript - 如何用另一个函数javascript停止计时器