问题描述
我使用的wxPython构建一个GUI和我试图调整一些文字,但它不工作。我想在三地对准三个不同的静态文本项(右对齐,居中对齐,左对齐)三个独立的小组。即我得到虽然其结果是,全部三个静态文本控制在各自面板的左上角的角对齐。
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.
这是code,我有:
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?
感谢您!
我运行的Mac OSX 10.7.2使用Python 2.7.1和wxPython的2.8.12.1
推荐答案
编辑:虽然一切评论下面的窗口工程,第一个选项是行不通的,比如,Ubuntu的,由于也许错误。在评论中发出的previous后表明,同样的问题在OSX发现。
在任何情况下,使用垂直的大小测定器的第二个选项无论在Ubuntu和Windows,所以你可以尝试它在OSX工作。
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.
您的文字有指示,你想要与 wx.ALIGN ...
的方式排列,事实上,它是对齐的。然而,静态文本的大小不是面板但文字的只是大小。已经限制了它的位置,以自己的大小,你看不到的排列方式之间的性差异。
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:
选项1。展开StaticText窗口小部件的大小和它的文本位置
Option 1. Expand the StaticText widget size and position your text on it
您可以使用它的尺寸
参数扩展您的StaticText窗口小部件的大小。这是除固定大小的父母或帧你是不会改变的大小或再使用其他应用程序中一个坏的解决方案。如果包含文本的修改后的文本的相对位置的小部件的大小也将被修改,因为它的大小保持固定。因此,它始终是最好用的大小测定器的方式来组织你的小工具。
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.
中的可用空间小部件占据了分级机槽的比例由第二个参数给出sizer.Add()
( 0
是最小的尺寸, 1
是全面占领):
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):
sizer_2.Add(self.label_1, 0, 0, 0)
要看到,只要你想,你必须告诉静态文本扩展到所有可用空间的面板对齐文本:
To see the text aligned in the panel as you want you have to tell the StaticText to expand to all the space available:
sizer_2.Add(self.label_1, 1, 0, 0)
在这里,您有相关的code:
Here you have the relevant code:
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
wx.Frame.__init__(self, *args, **kwds)
self.panel_1 = wx.Panel(self, -1)
self.label_1 = wx.StaticText(self.panel_1, -1, "label_1", style=wx.ALIGN_RIGHT)
self.panel_2 = wx.Panel(self, -1)
self.label_2 = wx.StaticText(self.panel_2, -1, "label_2", style=wx.ALIGN_CENTRE)
self.panel_3 = wx.Panel(self, -1)
self.label_3 = wx.StaticText(self.panel_3, -1, "label_3")
self.panel_1.SetBackgroundColour(wx.Colour(0, 255, 0))
self.panel_2.SetBackgroundColour(wx.Colour(0, 255, 255))
self.panel_3.SetBackgroundColour(wx.Colour(219, 112, 147))
sizer_1 = wx.BoxSizer(wx.HORIZONTAL)
sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
sizer_3 = wx.BoxSizer(wx.HORIZONTAL)
sizer_4 = wx.BoxSizer(wx.HORIZONTAL)
sizer_2.Add(self.label_1, 1, 0, 0)
sizer_3.Add(self.label_2, 1, 0, 0)
sizer_4.Add(self.label_3, 1, 0, 0)
self.panel_1.SetSizer(sizer_2)
self.panel_2.SetSizer(sizer_3)
self.panel_3.SetSizer(sizer_4)
sizer_1.Add(self.panel_1, 1, wx.EXPAND, 0)
sizer_1.Add(self.panel_2, 1, wx.EXPAND, 0)
sizer_1.Add(self.panel_3, 1, wx.EXPAND, 0)
self.SetSizer(sizer_1)
sizer_1.Fit(self)
self.Layout()
请注意在code长于必要的,以模拟天生的例子有三个小组。您获得使用一个唯一的面板相同的框架图。事实上,它可以不使用板和直接在分级机设置的StaticText进一步简化:
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:
class MyFrame2(wx.Frame):
def __init__(self, *args, **kwds):
wx.Frame.__init__(self, *args, **kwds)
self.label_1 = wx.StaticText(self, -1, "label_1", style=wx.ALIGN_RIGHT)
self.label_2 = wx.StaticText(self, -1, "label_2", style=wx.ALIGN_CENTRE)
self.label_3 = wx.StaticText(self, -1, "label_3")
self.label_1.SetBackgroundColour(wx.Colour(127, 255, 0))
self.label_2.SetBackgroundColour(wx.Colour(0, 255, 255))
self.label_3.SetBackgroundColour(wx.Colour(219, 112, 147))
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(self.label_1, 1, wx.EXPAND, 0)
sizer.Add(self.label_2, 1, wx.EXPAND, 0)
sizer.Add(self.label_3, 1, wx.EXPAND, 0)
self.SetSizer(sizer)
sizer.Fit(self)
self.Layout()
选项2。找到小部件本身在尺寸设定器的可用空间所需的位置。您可以使用位置
参数用于这一目的。但是,这将具有与上述使用静态文本
尺寸
的同样的问题。如此反复,你想控制与大小测定你的观点几何。您使用的一个在分级机定位插件:
Option 2. Locate the widget itself in the desired position at the available space of the sizer.You could use the position
parameter of StaticText
for that purpose. But this would have the same problems indicated above for the use of size
. So again you want to control the geometry of your views with sizers.You position the widget in the sizer using one of:
sizer_6.Add(self.label_5, 0, wx.ALIGN_RIGHT, 0)
或
sizer_7.Add(self.label_6, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
由于某种原因,这个工作你需要一个垂直的 BoxSizer
(并以同样的方式,如果你想使用wx.ALIGN_CENTER_VERTICAL您将需要一个水平 BoxSizer
:
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 horizontal BoxSizer
:
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
wx.Frame.__init__(self, *args, **kwds)
self.panel_4 = wx.Panel(self, -1)
self.label_5 = wx.StaticText(self.panel_4, -1, "label_5")
self.panel_5 = wx.Panel(self, -1)
self.label_6 = wx.StaticText(self.panel_5, -1, "label_6")
self.panel_6 = wx.Panel(self, -1)
self.label_7 = wx.StaticText(self.panel_6, -1, "label_7")
self.panel_4.SetBackgroundColour(wx.Colour(0, 255, 255))
self.panel_5.SetBackgroundColour(wx.Colour(127, 255, 0))
self.panel_6.SetBackgroundColour(wx.Colour(219, 112, 219))
sizer_1 = wx.BoxSizer(wx.HORIZONTAL)
sizer_8 = wx.BoxSizer(wx.VERTICAL)
sizer_7 = wx.BoxSizer(wx.VERTICAL)
sizer_6 = wx.BoxSizer(wx.VERTICAL)
sizer_6.Add(self.label_5, 0, wx.ALIGN_RIGHT, 0)
sizer_7.Add(self.label_6, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
sizer_8.Add(self.label_7, 0, 0, 0)
self.panel_4.SetSizer(sizer_6)
self.panel_5.SetSizer(sizer_7)
self.panel_6.SetSizer(sizer_8)
sizer_1.Add(self.panel_4, 1, wx.EXPAND, 0)
sizer_1.Add(self.panel_5, 1, wx.EXPAND, 0)
sizer_1.Add(self.panel_6, 1, wx.EXPAND, 0)
self.SetSizer(sizer_1)
sizer_1.Fit(self)
self.Layout()
这个选项意味着产生code表示更难以简化比示出了第一个选项板和大小测定器的组合。
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.
这篇关于为什么我的文字没有正确对齐在wxPython中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!