0

如果你通过 wxpython 创建一个框架,并改变了 staticText 的内容,对齐就会被初始化。我怎么解决这个问题?我想再次对齐它。

4

2 回答 2

2

这个问题是由文本控件的自动调整大小引起的,为了防止只需在样式中添加 wx.ST_NO_AUTORESIZE 即可解决。

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

静态文本没有名为 SetStyle() 的方法

于 2018-04-02T23:27:52.613 回答
0

要动态设置窗口对齐标志,您必须

  1. 获取 wx.Sizer
  2. 找到 wx.SizerItem
  3. 通过设置 wx.SizerItem 标志SetFlag
  4. 称呼Sizer.Layout()

这是一个简单的例子:

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()
于 2017-02-19T16:46:21.697 回答