如何动态更新多个wxpython静态文本?

how to dynamically update multiple wxpython static text?

我想动态更新多个静态文本,因为我只知道如何更新一个静态文本。

下面是我的代码:

import os
import time
import datetime

current_time = datetime.datetime.strftime(datetime.datetime.now(), '%d-  %m-%Y %H:%M:%S')
try:
    import wx
except ImportError:
    raise ImportError, "The wxPython module is required to run this   program."

class simpleapp_wx(wx.Frame):
    def __init__(self,parent,id,title):
        wx.Frame.__init__(self,parent,id,title, size =(500,300))
        self.SetBackgroundColour(wx.BLUE)
        self.parent = parent
        self.initialize()

    def initialize(self):
        sizer = wx.GridBagSizer()
        font = wx.Font(20, wx.DECORATIVE, wx.ITALIC, wx.NORMAL)
        self.SetFont(font)


        self.label = wx.StaticText(self,-1,label=u'Time Updated - 1:  {}'.format(current_time))
        self.label.SetBackgroundColour(wx.BLUE)
        self.label.SetForegroundColour(wx.WHITE)
        sizer.Add(self.label, (4,0),(1,5),wx.EXPAND)
        self.on_timer()

        self.SetSizer(sizer)
        self.Show(True)

    def on_timer(self):
        current_time =  datetime.datetime.strftime(datetime.datetime.now(), '%d-%m-%Y %H:%M:%S')
        self.label.SetLabel(label=u'Time Updated -1 : {}'.format(current_time))
        wx.CallLater(1000, self.on_timer)

if __name__ == "__main__":
    app = wx.App()
    frame = simpleapp_wx(None,-1,'Rain Sensor')
    app.MainLoop()

我试图在同一个 class 中添加另一个静态文本并创建另一个 CallLater,但它只更新最后一个静态文本...

下面的代码在我的机器上运行良好,Win7 x86,wxPython 3.0.2

import os
import time
import datetime

current_time = datetime.datetime.strftime(datetime.datetime.now(), '%d-  %m-%Y %H:%M:%S')
try:
    import wx
except ImportError:
    raise ImportError, "The wxPython module is required to run this   program."

class simpleapp_wx(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(500, 300))
        self.SetBackgroundColour(wx.BLUE)
        self.parent = parent
        self.initialize()

    def initialize(self):
        sizer = wx.GridBagSizer()
        font = wx.Font(20, wx.DECORATIVE, wx.ITALIC, wx.NORMAL)
        self.SetFont(font)

        self.label = wx.StaticText(self, -1, label=u'Time Updated - 1:  {}'.format(current_time))
        self.label2 = wx.StaticText(self, -1, label=u'Time Updated - 2:  {}'.format(current_time))
        self.label.SetBackgroundColour(wx.BLUE)
        self.label.SetForegroundColour(wx.WHITE)
        self.label.SetBackgroundColour(wx.BLUE)
        self.label2.SetForegroundColour(wx.WHITE)
        sizer.Add(self.label, (4, 0), (1, 5), wx.EXPAND)
        sizer.Add(self.label2, (5, 0), (1, 5), wx.EXPAND)
        self.on_timer()
        self.on_timer2()

        self.SetSizer(sizer)
        self.Show(True)

    def on_timer(self):
        current_time = datetime.datetime.strftime(datetime.datetime.now(), '%d-%m-%Y %H:%M:%S')
        self.label.SetLabel(label=u'Time Updated -1 : {}'.format(current_time))
        wx.CallLater(1000, self.on_timer)

    def on_timer2(self):
        current_time = datetime.datetime.strftime(datetime.datetime.now(), '%d-%m-%Y %H:%M:%S')
        self.label2.SetLabel(label=u'Time Updated -2 : {}'.format(current_time))
        wx.CallLater(1000, self.on_timer2)

if __name__ == "__main__":
    app = wx.App()
    frame = simpleapp_wx(None, -1, 'Rain Sensor')
    app.MainLoop()

我刚刚创建了另一个标签 label2,将其放入 sizer 并创建了另一个 on_timer2 方法并使用 wx.CallLater

调用