【问题标题】:What is the correct way to change StaticText label?更改 StaticText 标签的正确方法是什么?
【发布时间】:2013-04-04 14:53:23
【问题描述】:

我正在编写一个 wxPython 应用程序,当我尝试更改 StaticText 对象中的文本时,我设置的对齐方式消失了。它从居中开始,但在文本更改后,对齐方式又回到默认的左对齐方式。这是我的相关代码:

#initializing
self.panel = wx.Panel(self)
self.st_RouteInfo = wx.StaticText(self.panel, label=self.route_start, style=wx.ALIGN_CENTRE)

#changing StaticText
self.st_RouteInfo.SetLabel("Put text here")
self.Update()

我猜我忘记了一些基本的东西,因为我是 wxPython 和 wxWidgets 的新手。谢谢!

【问题讨论】:

    标签: wxpython wxwidgets


    【解决方案1】:

    您必须调用 sizer 或 parent 的 Layout() 方法:

    class MainWindow(wx.Frame):
        def __init__(self, *args, **kwargs):
            wx.Frame.__init__(self, *args, **kwargs)
    
            self.panel = wx.Panel(self)
            self.label = wx.StaticText(self.panel, label="Test", style=wx.ALIGN_CENTRE)
            self.button = wx.Button(self.panel, label="Change")
    
            self.sizer = wx.BoxSizer()
            self.sizer.Add(self.label, 1)
            self.sizer.Add(self.button)
    
            self.button.Bind(wx.EVT_BUTTON, self.OnButton)
    
            self.panel.SetSizerAndFit(self.sizer)  
            self.Show()
    
        def OnButton(self, e):
            self.label.SetLabel("Oh, this is very looooong!")
            self.sizer.Layout()
            # self.panel.Layout()  #Either works
    
    app = wx.App(False)
    win = MainWindow(None)
    app.MainLoop()
    

    【讨论】:

    • 在我的例子中,我只有标签可用,所以我做了(C++ 等价于)label.GetParent().Layout()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-24
    • 2021-05-24
    • 1970-01-01
    • 2011-01-06
    相关资源
    最近更新 更多