wxPython:如何在 SetLabelText 之后更改对齐方式 StaticText
wxPython : How to change alignment StaticText after SetLabelText
如果你通过wxpython创建一个frame,改变staticText的内容,对齐会被初始化。
我怎么解决这个问题?
我想再对齐一下。
要动态设置 window 对齐标志,您必须
- 获取wx.Sizer
- 找到 wx.Sizer项目
- 通过
SetFlag
设置wx.Sizer项目标志
- 致电
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()
这个问题是由于文本控件的自动调整大小引起的,所以为了防止只是在样式中添加wx.ST_NO_AUTORESIZE就可以解决。
txtCtrl = wx.StaticText(parent, -1, "some label", style = wx.ALIGN_CENTER| wx.ST_NO_AUTORESIZE)
静态文本没有名为 SetStyle() 的方法
如果你通过wxpython创建一个frame,改变staticText的内容,对齐会被初始化。 我怎么解决这个问题? 我想再对齐一下。
要动态设置 window 对齐标志,您必须
- 获取wx.Sizer
- 找到 wx.Sizer项目
- 通过
SetFlag
设置wx.Sizer项目标志
- 致电
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()
这个问题是由于文本控件的自动调整大小引起的,所以为了防止只是在样式中添加wx.ST_NO_AUTORESIZE就可以解决。
txtCtrl = wx.StaticText(parent, -1, "some label", style = wx.ALIGN_CENTER| wx.ST_NO_AUTORESIZE)
静态文本没有名为 SetStyle() 的方法