WxPython StaticText动态更新

时间:2013-02-05 01:18:33

标签: python user-interface wxpython

def update_text(self, info):
        index = {
            "username":self.username,
            "online status":self.online_status,
            "dial status":self.dialing_status,
            "dtmf transfer number":self.DTMF_t_num,
            "dtmf number":self.DTMF_num,
            "program status":self.program_status,
            "credits":self.credits}
        text = index[info[0]]
        data = info[1]
        append = info[2]
        if append:
            current = text.GetLabel()
            text.SetLabel(current + " " +data)
        else:
            text.SetLabel(data)

为什么不更新它所指的wx.StaticText? “index”中的每个变量都指静态文本。

1 个答案:

答案 0 :(得分:2)

没有一些可运行的代码,很难说。我做了我自己的事情并且让它工作了:

import random
import wx

########################################################################
class MyPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)

        self.username = wx.StaticText(self, label="username")
        self.online_status = wx.StaticText(self, label="offline")
        self.dialing_status = wx.StaticText(self, label="no dial tone")

        btn = wx.Button(self, label="Update")
        btn.Bind(wx.EVT_BUTTON, self.onUpdate)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.username, 0, wx.ALL, 5)
        sizer.Add(self.online_status, 0, wx.ALL, 5)
        sizer.Add(self.dialing_status, 0, wx.ALL, 5)
        sizer.Add(btn, 0, wx.ALL, 5)
        self.SetSizer(sizer)

    #----------------------------------------------------------------------
    def update_text(self, info):
        """"""
        index = {
            "username":self.username,
            "online status":self.online_status,
            "dial status":self.dialing_status
            }
        text = index[info[0]]
        data = info[1]
        append = info[2]
        if append:
            current = text.GetLabel()
            text.SetLabel(current + " " + data)
        else:
            text.SetLabel(data)

    #----------------------------------------------------------------------
    def onUpdate(self, event):
        """"""
        info = random.choice(
            [("username", "mork89", 1),
             ("online status", "online", 0),
             ("dial status", "dialing", 1)
             ])
        self.update_text(info)

########################################################################
class MainFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Dynamic StaticText")
        panel = MyPanel(self)
        self.Show()

if __name__ == "__main__":
    app = wx.App(False)
    frame = MainFrame()
    app.MainLoop()

也许我的代码可以帮助你找出你的代码。