【问题标题】:how to dynamically update multiple wxpython static text?如何动态更新多个 wxpython 静态文本?
【发布时间】:2016-10-26 08:32:18
【问题描述】:

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

下面是我的代码:

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,但它只更新了最后一个静态文本...

【问题讨论】:

    标签: python wxpython


    【解决方案1】:

    下面的代码在我的机器上运行良好,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调用

    【讨论】:

    • 感谢我还是 wxpython 的新手。现在我知道如何从这个示例代码更新我的项目....
    • @anubismmt 如果这回答了你的问题,你应该接受它和/或投票它是有用的。见stackoverflow.com/help/accepted-answer
    • 我很抱歉,因为我上周才加入这个社区,所以我仍然是堆栈溢出的新手。
    猜你喜欢
    • 2017-03-09
    • 1970-01-01
    • 2010-11-20
    • 2011-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多