wxPython:如何在SetLabelText之后更改对齐StaticText

时间:2017-02-17 17:57:55

标签: python wxpython wxformbuilder

如果通过wxpython创建框架并更改staticText的内容,则将初始化对齐。 我怎么解决这个问题? 我想再次对齐它。

2 个答案:

答案 0 :(得分:0)

要动态设置窗口对齐标记

  1. 获取wx.Sizer
  2. 找到wx.SizerItem
  3. 通过SetFlag
  4. 设置wx.SizerItem标志
  5. 致电Sizer.Layout()
  6. 这是一个简单的例子:

    import wx
    import traceback
    
    
    def run():
        app = wx.App()
        # create the test frame
        frame = wx.Frame(None, title="test frame", size=(500, 500))
    
        # create a simple boxsizer
        sizer = wx.BoxSizer()
        # create the object that we'll be dynamically adjusting
        st = wx.StaticText(frame, label="Click me")
        st.SetFont(wx.Font(30, 74, 90, wx.FONTWEIGHT_BOLD, True, "Arial Rounded"))
        # align the text to the middle initially
        sizer.Add(st, 0, wx.ALIGN_CENTER_VERTICAL)
        frame.SetSizer(sizer)
    
        # bind to an arbitrary event
        st.Bind(wx.EVT_LEFT_DOWN, on_click)
        # do the initial layout and show the frame
        frame.Layout()
        frame.Show()
        app.MainLoop()
    
    
    def on_click(event):
        event.Skip()
        # retrieving the static text object
        st = event.GetEventObject()  # type: wx.StaticText
        # get the sizer that contains this object
        sizer = st.GetContainingSizer()  # type: wx.BoxSizer
        # find the sizer item
        # the sizer item holds the alignment information and tells
        # the sizer how to display this object
        sizer_item = sizer.GetItem(st)  # type: wx.SizerItem
        # alternate between aligning at the top & bottom
        if sizer_item.GetFlag() & wx.ALIGN_BOTTOM:
            print("Setting alignment to top")
            sizer_item.SetFlag(wx.ALIGN_TOP)
        else:
            print("Setting alignment to bottom")
            sizer_item.SetFlag(wx.ALIGN_BOTTOM)
        # call Layout to recalculate the object positions
        sizer.Layout()
    
    
    if __name__ == "__main__":
        try:
            run()
        except:
            traceback.print_exc()
            input()
    

答案 1 :(得分:0)

这个问题是由文本控件的自动调整引起的,所以为了防止只是将wx.ST_NO_AUTORESIZE添加到样式中它就会被解决。

txtCtrl = wx.StaticText(parent, -1, "some label", style = wx.ALIGN_CENTER| wx.ST_NO_AUTORESIZE)

静态文本没有名为SetStyle()

的方法