简体   繁体   中英

wxPython: How to fix the size of one part of a grid sizer

What I want to do is create 3 column that will expand as the window expands. But I want to put limits on it so the different columns expand to different maximum amounts.

What I have done here is used wxGlade to create a frame w/ a grid sizer that has 1 row and three columns. I want Part1 to have a fixed width of 'A', Part2 to have a fixed width of 'B' and Part3 to be able to expand as normal.

How do I do that?

#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-
# generated by wxGlade 0.6.3 on Tue May 24 11:35:21 2011

import wx

# begin wxGlade: extracode
# end wxGlade



class MyFrame1(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MyFrame1.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.panel_1 = wx.Panel(self, -1)
        self.sizer_10_staticbox = wx.StaticBox(self.panel_1, -1, "Part2")
        self.sizer_11_staticbox = wx.StaticBox(self.panel_1, -1, "Part3")
        self.sizer_9_staticbox = wx.StaticBox(self.panel_1, -1, "Part1")

        self.__set_properties()
        self.__do_layout()
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: MyFrame1.__set_properties
        self.SetTitle("frame_2")
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: MyFrame1.__do_layout
        sizer_8 = wx.BoxSizer(wx.VERTICAL)
        grid_sizer_4 = wx.GridSizer(1, 3, 0, 0)
        sizer_11 = wx.StaticBoxSizer(self.sizer_11_staticbox, wx.HORIZONTAL)
        sizer_10 = wx.StaticBoxSizer(self.sizer_10_staticbox, wx.HORIZONTAL)
        sizer_9 = wx.StaticBoxSizer(self.sizer_9_staticbox, wx.HORIZONTAL)
        grid_sizer_4.Add(sizer_9, 1, wx.EXPAND, 0)
        grid_sizer_4.Add(sizer_10, 1, wx.EXPAND, 0)
        grid_sizer_4.Add(sizer_11, 1, wx.EXPAND, 0)
        self.panel_1.SetSizer(grid_sizer_4)
        sizer_8.Add(self.panel_1, 1, wx.EXPAND, 0)
        self.SetSizer(sizer_8)
        sizer_8.Fit(self)
        self.Layout()
        # end wxGlade

# end of class MyFrame1


if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    frame_2 = MyFrame1(None, -1, "")
    app.SetTopWindow(frame_2)
    frame_2.Show()
    app.MainLoop()

If you're using a GridSizer I'd probably suggest you take a look at GridBagSizer instead.

Once you decide if that'll be usable, then you probably need to look into the sizer.AddGrowableRow(row) and sizer.AddGrowableCol(col) methods. They allow you to indicate columns or rows in the grid that expand. They don't use use proportions like you're familiar with from the BoxSizers (so if two columns grow they both grow at the same rate) but you can set some that expand and others that don't.

Indexes are zero based, so by your description I'd guess something like gridbagsizer_4.AddGrowableCol(2) , but like my comment says, wxGlade's naming conventions make me ill and it's hard to tell for certain, and those methods won't be available on the GridSizer.

For any interested parties, there's a post covering my form building and form dialog classes available here: Form Builder . It was easier to post it on my site where I could include attachments than to try to paste it all here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM