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

时间:2016-10-26 08:32:19

标签: python wxpython

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

以下是我的代码:

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

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

1 个答案:

答案 0 :(得分:0)

以下代码适用于我的机器,Win7 x86,wxPython 3.0.2

label2

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

进行了调用