Zack
Zack

Reputation: 4375

wxpython: how would I configure a box sizer so that it has a center column that moves on resize, but doesn't change dimensions(pic explanation inside)

I can't figure out how to nest my sizers so that I get the following behavior.

enter image description here

When a user resizes the window, I would like for the configuration of all my widgets to stay the same, but have the central column housing everything to stay centered.

Upvotes: 3

Views: 1041

Answers (1)

Bouke
Bouke

Reputation: 12138

Using a box sizer, you should adjust the proportion accordingly. The spacers on the side should fill the remaining space, while the middle part should have a fixed amount of space. See the documentation of wx.BoxSizer:

It is the unique feature of a box sizer, that it can grow in both directions (height and width) but can distribute its growth in the main direction (horizontal for a row) unevenly among its children. This is determined by the proportion parameter give to items when they are added to the sizer. It is interpreted as a weight factor, i.e. it can be zero, indicating that the window may not be resized at all, or above zero. If several windows have a value above zero, the value is interpreted relative to the sum of all weight factors of the sizer, so when adding two windows with a value of 1, they will both get resized equally and each will receive half of the available space after the fixed size items have been sized.

See also this small example code (generated using wxFormBuilder). I've emphasized that the spacers have a proportion of 1 and the app has 0.

class MyFrame1 (wx.Frame):
    def __init__(self):
        super(MyFrame1, self).__init__()

        fluid_sizer = wx.BoxSizer(wx.HORIZONTAL)
        fluid_sizer.AddSpacer((0, 0), 1, wx.EXPAND, 5)
        #                             ^--- proportion = 1

        self.fixed_panel = wx.Panel(self, wx.ID_ANY, wx.DefaultPosition, wx.Size(-1,-1), wx.TAB_TRAVERSAL)
        self.fixed_panel.SetBackgroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_HIGHLIGHT))
        fixed_sizer = wx.BoxSizer(wx.VERTICAL)
        fixed_sizer.SetMinSize(wx.Size(150,-1)) 
        self.m_button1 = wx.Button(self.fixed_panel, wx.ID_ANY, u"MyButton", wx.DefaultPosition, wx.DefaultSize, 0)
        fixed_sizer.Add(self.m_button1, 0, wx.ALL, 5)
        self.fixed_panel.SetSizer(fixed_sizer)
        self.fixed_panel.Layout()
        fixed_sizer.Fit(self.fixed_panel)

        fluid_sizer.Add(self.fixed_panel, 0, wx.EXPAND |wx.ALL, 5)
        #                                 ^--- proportion = 0


        fluid_sizer.AddSpacer((0, 0), 1, wx.EXPAND, 5)
        #                             ^--- proportion = 1

        self.SetSizer(fluid_sizer)
        self.Layout()

Preview of example code

Upvotes: 4

Related Questions