Placing elements (panels) within a wx.GridBagSizer
I'm using a gridbagsizer to place two panels within a frame.
control_panel = wx.Panel(self, id=ID_CONTROL_PANEL)
main_panel = wx.Panel(se开发者_JAVA技巧lf, id=ID_MAIN_PANEL)
frame_sizer = wx.GridBagSizer(1, 1)
frame_sizer.Add(control_panel, (0, 0), (2, 5), flag=wx.EXPAND)
frame_sizer.Add(main_panel, (2, 0), (5, 5), flag=wx.EXPAND)
frame_sizer.AddGrowableRow(0)
frame_sizer.AddGrowableCol(0)
self.SetSizer(frame_sizer)
I want the control_panel to be above the main_panel (as you can see above). However this is the output I get:
I don't know why the top panel is so much larger than the bottom panel, when I've specified the opposite. (2, 5) for the top and (5, 5) for the bottom. It also has a strange behaviour when I resize it smaller, it basically gives me what I want (see image below), but I don't know how to maintain the ratio when I make it larger again.
However when you start resizing larger (even by a small amount) you can see (below) the shapes and ratio change dramatically in the other direction (with the bottom becoming much smaller and the top much larger).
Still trying to learn this sizers and can't really find this problem with any of the examples I've seen. Also tried experimenting with all the parameters and doing some tests and I can't figure out what's going on here. Any help would be much appreciated.
EDIT: Image below added to aid comment below.
Personally, I would use a BoxSizer in VERTICAL orientation. Here's a simple example:
import wx
class MyForm(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial")
# Add a panel so it looks the correct on all platforms
panel = wx.Panel(self, wx.ID_ANY)
control_panel = wx.Panel(panel)
control_panel.SetBackgroundColour("Yellow")
main_panel = wx.Panel(panel)
main_panel.SetBackgroundColour("Blue")
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(control_panel, 3, wx.EXPAND)
sizer.Add(main_panel, 1, wx.EXPAND)
panel.SetSizer(sizer)
# Run the program
if __name__ == "__main__":
app = wx.PySimpleApp()
frame = MyForm().Show()
app.MainLoop()
I changed the background color of each panel to make it easier to tell which is which. Note that I gave the control panel a proportion of 3 and the main panel a proportion of 1. You might want to take a look at the Widget Inspection Tool as well to help you visualize it:
http://wiki.wxpython.org/Widget%20Inspection%20Tool
Of the seven rows and five columns in the grid you specified only the first row and the first column to be growable.
The control panel occupies the first row so it expands to take up the available space.
The wx.EXPAND flag only has an effect if the cell is in a growable row or column.
精彩评论