我正在尝试为Python设计WX表单生成器中的仪表板。我在尝试弄清楚如何使作为子级的两个水平尺寸调整器到垂直尺寸调整器之间彼此分开时遇到麻烦。以下是描述我所指的屏幕截图:
蓝色箭头表示我希望文本框控件和标签向上移动到第一个标签附近。
My first instinct is that is had something to do with the wx.EXPAND etc flags however, I was not able to change those in a manner that made the sizers come closer to each other. It's almost like every time I place a sizer it automatically tries to fit everything in the entire window...which makes it difficult to place items in precise locations on the form. Any suggestions on how to stop sizers from expanding to the entire frame window size?
My next course of action was to try and use a gridsizer or a flex grid sizer however, I've only used them through direct code, where you can select the exact location in the grid where you want to add a widget or object. With form builder, I'm finding that they are more difficult to use mainly because I can't insert objects at certain indices in the grid sizer. It inserts them sequentially:
1,1 -> 1,2 -> 1,3 -> 1,i -> 2,1 -> 2,2 -> 2,3 -> 2,i -> j,i
Which means that if I need to change something in the grid...Its very difficult. Is there something I'm missing that makes it easier to insert objects into the grid, other than sequentially?
Below is one of example of 5x5 grid sizer (not completely filled) where I have specified both the horizontal and vertical gaps = 0:
Besides being able to replace the button with a textbox at 1,4, I notice that the horizontal gap is clearly not zero even though I specified 0 for vertical and horizontal gaps. This also makes it very hard to design the form. Why is there a horizontal gap between buttons?
As you have noticed "stuff" expands to fit when using sizers and the proportion flag can wreak havoc with your layout if not used properly.
Play with this simple bit of code:
import wx
class Summary(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Playing with Simple Sizers", size=(430,260))
self.panel = wx.Panel(self, wx.ID_ANY)
self.log = wx.TextCtrl(self.panel, wx.ID_ANY, value="input1:",size=(428,25))
self.log2 = wx.TextCtrl(self.panel, wx.ID_ANY, value="input2:", size=(428,25))
self.quit_button = wx.Button(self.panel, label="Quit",size=(60,25))
self.button1= wx.Button(self.panel, label="1",)
self.button2 = wx.Button(self.panel, label="2",size=(60,25))
self.button3 = wx.Button(self.panel, label="3",size=(60,25))
self.button4 = wx.Button(self.panel, label="4",size=(60,25))
self.quit_button.Bind(wx.EVT_BUTTON, self.OnQuit)
vbox = wx.BoxSizer(wx.VERTICAL)
hbox1 = wx.BoxSizer(wx.HORIZONTAL)
hbox2 = wx.BoxSizer(wx.HORIZONTAL)
hbox3 = wx.BoxSizer(wx.HORIZONTAL)
hbox4 = wx.BoxSizer(wx.HORIZONTAL)
vbox.Add(self.quit_button, 0, wx.ALL|wx.EXPAND, 1)
hbox1.Add(self.log, 0, wx.ALL|wx.EXPAND, 1)
hbox2.Add(self.log2, 0, wx.ALL|wx.EXPAND, 1)
hbox3.Add(self.button1, 1, wx.ALL|wx.EXPAND, 1)
hbox3.Add(self.button2, 2, wx.ALL|wx.EXPAND, 1)
hbox4.Add(self.button3, 0, wx.ALL|wx.EXPAND, 1)
hbox4.Add(self.button4, 1, wx.ALL|wx.EXPAND, 1)
vbox.Add(hbox1, 0, wx.ALIGN_RIGHT|wx.EXPAND, 1)
vbox.Add(hbox3, 0, wx.ALIGN_RIGHT|wx.EXPAND, 1)
vbox.Add(hbox4, 0, wx.ALIGN_RIGHT|wx.EXPAND, 1)
vbox.Add(hbox2, 0, wx.ALIGN_RIGHT|wx.EXPAND, 1)
self.panel.SetSizer(vbox)
self.Show()
def OnQuit(self, event):
self.Close()
# Run the program
if __name__ == "__main__":
app = wx.App()
frame = Summary()
app.MainLoop()
比例标记是添加到sizer中时的第二个参数。
您将注意到按钮的设置为0、1和2,对其进行更改并查看按钮之间的关系,并使用按钮的size参数进行播放。
将self.log或self.log2上的比例标志更改为1,然后观察其扩展。
最后,使框架尺寸变宽,然后看一下反应。
大小调整器一开始可能非常令人沮丧,但是一旦“获得”,它们就是强大的工具。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句