简体   繁体   English

默认情况下,wxPython小部件的间距不正确

[英]wxPython widgets are spaced incorrectly by default

I am having problems spacing widgets in wxPython. 我在wxPython中间距小部件时遇到问题。

In the code below, the buttons in Vbox2 and Vbox3 are squished by default, and I don't understand why. 在下面的代码中,默认情况下压缩了Vbox2和Vbox3中的按钮,我不明白为什么。 I thought that a BoxSizer should, by default, expand to fit all of its contents. 我认为BoxSizer在默认情况下应该扩展以适合其所有内容。

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. 将hbox添加到主sizer时,删除wx.SHAPED标志。 That will allow the shape (aspect ratio) of the the hbox to change to match what is needed by its contents. 这将允许hbox的形状(纵横比)发生变化,以匹配其内容所需的内容。 In typical layouts there are very few times when you'll need to use wx.SHAPED 在典型布局中,很少需要使用wx.SHAPED

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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