1

I want to dynamically update multiple static texts as I only know how to update only one static text.

Below is my code:

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

I have tried to add another static text in the same class and create another CallLater, but it only update the last static text...

1 Answer 1

1

The code below works well on my machine, 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()

I have just created another label label2, put it in the sizer and created another on_timer2 method and called with wx.CallLater

Sign up to request clarification or add additional context in comments.

3 Comments

thank i still new to wxpython. now i know how to do my update my project from this example code....
@anubismmt If this answers your question, you should accept it and/or up vote it as useful. See stackoverflow.com/help/accepted-answer
I'm sorry as I still new in stack overflow as i just join this community last week.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.