简体   繁体   中英

wxPython widgets are spaced incorrectly by default

I am having problems spacing widgets in wxPython.

In the code below, the buttons in Vbox2 and Vbox3 are squished by default, and I don't understand why. I thought that a BoxSizer should, by default, expand to fit all of its contents.

My question is: what do I need to change to prevent this crowding of buttons?

Here is the full code to run my example:

import wx
import sys


class GridFrame(wx.Frame):

    def __init__(self, frame_name="grid frame",
                 panel_name="grid panel", parent=None):
        title = 'Edit {} data'.format(panel_name)
        super(GridFrame, self).__init__(parent=parent, id=wx.ID_ANY,
                                        name=frame_name, title=title)
        self.remove_cols_mode = False
        self.panel = wx.Panel(self, name=panel_name)#, size=wx.GetDisplaySize())
        self.grid_type = str(panel_name)
        self.InitUI()


    ## Initialization functions
    def InitUI(self):
        """
        initialize window
        """
        self.main_sizer = wx.BoxSizer(wx.VERTICAL)
        set1, set2, set3 = [], [], []
        ## btns set 1
        for btn_name in ['btn1', 'btn1a']:
            btn = wx.Button(self.panel, label=btn_name, name=btn_name)
            set1.append((btn))
        ## Btns set 2
        for btn_name in ['btn2', 'btn3', 'btn4']:
            btn = wx.Button(self.panel, label=btn_name, name=btn_name)
            set2.append((btn))
        ## btns set 3
        for btn_name in ['btn5', 'btn6', 'btn7']:
            btn = wx.Button(self.panel, label=btn_name, name=btn_name)
            set3.append((btn))

        ## Add content to sizers
        self.hbox = wx.BoxSizer(wx.HORIZONTAL)
        vbox1 = wx.StaticBoxSizer(wx.StaticBox(self.panel, -1, label='Vbox1'), wx.VERTICAL)
        vbox2 = wx.StaticBoxSizer(wx.StaticBox(self.panel, -1, label='Vbox2'), wx.VERTICAL)
        vbox3 = wx.StaticBoxSizer(wx.StaticBox(self.panel, -1, label='Vbox3'), wx.VERTICAL)
        for btn in set1:
            vbox1.Add(btn, flag=wx.ALL, border=5)
        for btn in set2:
            vbox2.Add(btn, flag=wx.ALL, border=5)
        for btn in set3:
            vbox3.Add(btn, flag=wx.ALL, border=5)
        self.hbox.Add(vbox1, flag=wx.ALL, border=5)
        self.hbox.Add(vbox2, flag=wx.ALL, border=5)
        self.hbox.Add(vbox3, flag=wx.ALL, border=5)

        # final layout, set size
        self.main_sizer.Add(self.hbox, flag=wx.ALL|wx.ALIGN_CENTER|wx.SHAPED, border=20)
        self.main_sizer.AddSpacer(20)
        self.panel.SetSizer(self.main_sizer)
        self.panel_sizer = wx.BoxSizer(wx.VERTICAL)
        self.panel_sizer.Add(self.panel, 1)
        self.SetSizer(self.panel_sizer)
        self.panel_sizer.Fit(self)
        self.Centre()
        self.Show()


if __name__ == "__main__":
    app = wx.App()
    frame = GridFrame()
    if '-i' in sys.argv:
        import wx.lib.inspection
        wx.lib.inspection.InspectionTool().Show()
    app.MainLoop()

Remove the wx.SHAPED flag when adding the hbox to the main sizer. That will allow the shape (aspect ratio) of the the hbox to change to match what is needed by its contents. In typical layouts there are very few times when you'll need to use wx.SHAPED

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