为什么我的文本在 wxPython 中无法正确对齐?
我正在使用 wxPython 构建 GUI,并尝试对齐一些文本,但它根本不起作用。 我正在尝试在三个单独的面板中的三个位置(右对齐、居中对齐和左对齐)对齐三个不同的静态文本项。我得到的结果是所有三个静态文本控件都在各自面板的左上角对齐。
这是我的代码:
self.lastText=wx.StaticText(self.textDisplayPanel1, label=self.lastWords, style=wx.ALIGN_RIGHT)
self.currentText=wx.StaticText(self.textDisplayPanel2, label=self.currentWords, style=wx.ALIGN_CENTRE)
self.nextText=wx.StaticText(self.textDisplayPanel3, label=self.nextWords, style=wx.ALIGN_LEFT)
关于如何解决这个问题有什么想法吗?
谢谢你!
我正在运行带有 Python 2.7.1 和 wxPython 2.8.12.1 的 mac OSX 10.7.2
I'm using wxPython to build a GUI and I'm trying to align some text but it's not working at all.
I'm trying align three different static text items in three places (right aligned, center aligned, and left aligned) in three seperate panels. The result that I'm getting though is that all three static text controls are aligned in the top left corners of their respective panels.
This is code I have:
self.lastText=wx.StaticText(self.textDisplayPanel1, label=self.lastWords, style=wx.ALIGN_RIGHT)
self.currentText=wx.StaticText(self.textDisplayPanel2, label=self.currentWords, style=wx.ALIGN_CENTRE)
self.nextText=wx.StaticText(self.textDisplayPanel3, label=self.nextWords, style=wx.ALIGN_LEFT)
Any ideas on how I fix this?
Thank you!
I'm running mac OSX 10.7.2 with Python 2.7.1 and wxPython 2.8.12.1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑:虽然下面评论的所有内容都适用于 Windows,但第一个选项不适用于 Ubuntu,例如,由于可能存在错误。评论中给出的先前帖子表明在 OSX 中也发现了相同的问题。
无论如何,使用垂直调整器的第二个选项在 Ubuntu 和 Windows 中都适用,因此您可以在 OSX 上尝试。
您的文本具有按照您想要的方式与
wx.ALIGN...
对齐的指令,事实上,它是对齐的。然而,StaticText 的大小不是面板的大小,而只是文本的大小。将其位置限制为其自身大小后,您将无法看到对齐模式之间的差异。您有两种选择来解决该问题:
选项 1. 扩大 StaticText 小部件的大小并将文本放置在其上
您可以使用其
size
参数扩大 StaticText 小部件的大小。这是一个糟糕的解决方案,除了固定大小的父级或框架之外,您不会在其他应用程序中更改大小或重复使用。如果包含文本的小部件的大小发生变化,则文本的相对位置也将被修改,因为其大小保持固定。因此,最好通过 sizer 来组织您的小部件。小部件在 sizer 槽中占据的可用空间比例由
sizer.Add()
中的第二个参数给出(0
是最小大小,1< /code> 是完整占用):
要查看面板中对齐的文本,您必须告诉 StaticText 扩展到所有可用空间:
这里有相关代码:
请注意,代码比需要的长,以便用三个面板模仿您的示例。您仅使用一个面板即可获得相同的框架视图。事实上,它可以进一步简化,不使用面板并直接在 sizer 上设置 StaticText:
选项 2。 将小部件本身定位在 sizer 的可用空间中的所需位置。
您可以使用
StaticText
的position
参数来实现此目的。但这会出现与上面使用size
相同的问题。因此,您再次需要使用 sizer 来控制视图的几何形状。您可以使用以下之一将小部件放置在大小调整器中:
或
出于某种原因,要使其工作,您需要一个垂直的
BoxSizer
(并且以同样的方式,如果您想使用 wx.ALIGN_CENTER_VERTICAL,您将需要水平BoxSizer
:此选项意味着面板和尺寸调整器的组合,产生的代码比第一个选项显示的代码更难简化。
Edit: Although everything commented below works on windows, the first option would not work on, for example, Ubuntu due to maybe a bug. A previous post given in the comments indicate that the same problem is found in OSX.
In any case, the second option using vertical sizers works both in Ubuntu and windows so you could try it on OSX.
Your text has the instruction to align in the way you want with
wx.ALIGN...
and, in fact, it is aligned. However the size of the StaticText is not that of the panel but just the size of the text. Having restricted its position to its own size, you can not see the diference between the alignment modes.You have two options to solve the problem:
Option 1. Expand the StaticText widget size and position your text on it
You could expand the size of your StaticText widget using its
size
parameter. This is a bad solution except for fixed size parents or frames you are not going to change size or reuse in other applications. If the size of the widget containing the text changes then the relative position of the text will be also modified because its size stays fixed. So it is always better to organize your widgets by means of sizers.The proportion of the available space the widget occupies in the sizer slot is given by the second parameter in
sizer.Add()
(0
is minimal size,1
is full occupation):To see the text aligned in the panel as you want you have to tell the StaticText to expand to all the space available:
Here you have the relevant code:
Note the code is longer than needed in order to mimick your example with three panels. You obtain the same frame view using one only panel. In fact it could be simplified further not using panels and setting the StaticText directly on the sizer:
Option 2. Locate the widget itself in the desired position at the available space of the sizer.
You could use the
position
parameter ofStaticText
for that purpose. But this would have the same problems indicated above for the use ofsize
. So again you want to control the geometry of your views with sizers.You position the widget in the sizer using one of:
or
For some reason, for this to work you need a vertical
BoxSizer
(and in the same way, if you would like to use wx.ALIGN_CENTER_VERTICAL you will need an horizontalBoxSizer
:This option implies a combination of panels and sizers that produces a code that is more difficult to simplify than that shown for the first option.
我认为文本未对齐是因为未设置大小。尝试:
它将在 200*20 的盒子内对齐。默认情况下,大小为“自动”,即(-1,-1),即足以使文本可见,并且在该框中对齐不会产生可见效果。
但是,这并不总是有效。
I think that text is not aligned because the size is not set. Try:
And it will be aligned inside a 200*20 box. By default the size is "auto" i.e. (-1,-1) i.e. just enough for the text to be visible, and aligning in that box will not give visible effect.
However, this does not always work.
我昨天也遇到了同样的问题,我使用MAC OS X,wxpython 4.0.6。
最后我发现这个脚本对我有用。
我意识到如果你最后不 SetSizer,静态文本将不会居中对齐。
我的代码
I also had the same problem yesterday, I use MAC OS X, wxpython 4.0.6.
At last I find out this script works for me.
And I realise that if u don't SetSizer at last, the statictext will not align at center.
My code