首页 > 解决方案 > two BoxSizers interfere

问题描述

I have two BoxSizers, the first one

sizer = wx.BoxSizer (wx.VERTICAL)
sizer.Add (self.grid2, 1, wx.EXPAND)
panel2.SetSizer (sizer)

and another vertical BoxSizers, to the left of the button grid, both BoxSizers interfere.

vbox1 = wx.FlexGridSizer (rows = 1, cols = 3, hgap = 5, vgap = 5)
buttonsBox1 = wx.BoxSizer (wx.VERTICAL)
buttonsBox1.Add (self.buttonoborved)
vbox1.Add (buttonsBox1)
vbox1.Add (self.grid2)
vbox1.Add (midPan1, wx.ID_ANY, wx.EXPAND | wx.ALL, 20)
panel2.SetSizer (vbox1)

An error occurs - Adding a window already in a sizer, detach it first! How can they be called at the same time.

Edit: That are two BoxSizer, one in other, but how can be put buttons in there.

import wx
import wx.grid
from wx.lib.scrolledpanel import ScrolledPanel

class TestPanel(ScrolledPanel):
    def __init__(self, parent):
    ScrolledPanel.__init__(self, parent, wx.ID_ANY, size=(640, 480))
    self.sizer = wx.BoxSizer(wx.VERTICAL)
    self.sizer.Add(self._create_table(), 1, wx.EXPAND | wx.ALL, 5)
    self.SetSizer(self.sizer)
    self.SetupScrolling()
    self.SetAutoLayout(1)

def _create_table(self):
    _table = wx.grid.Grid(self, -1)
    _table.CreateGrid(0, 2)
    for i in range(1723):  # Work normally If I use 1722 rows
        _table.AppendRows()
        _table.SetCellValue(i, 0, str(i))
    return _table

class TestFrame(wx.Frame):
    def __init__(self):
    wx.Frame.__init__(self, None, wx.ID_ANY,
            title="Scroll table", size=(640, 480))
    self.fSizer = wx.BoxSizer(wx.VERTICAL)
    self.fSizer.Add(TestPanel(self), 1, wx.EXPAND)
    self.SetSizer(self.fSizer)
    self.Show()

if __name__ == "__main__":
    app = wx.App(False)
    frame = TestFrame()
    app.MainLoop()

标签: pythonpython-3.xwxpython

解决方案


It depends where you want to place the buttons but let's assume they should be at the bottom.

import wx
import wx.grid
from wx.lib.scrolledpanel import ScrolledPanel

class TestPanel(ScrolledPanel):
    def __init__(self, parent):
        ScrolledPanel.__init__(self, parent, wx.ID_ANY, size=(640, 480))
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self._create_table(), 1, wx.EXPAND | wx.ALL, 5)

        self.b1 = wx.Button(self, -1, "Button 1")
        self.b2 = wx.Button(self, -1, "Button 2")
        self.b3 = wx.Button(self, -1, "Button 3")

        button_sizer = wx.BoxSizer(wx.HORIZONTAL)
        button_sizer.Add(self.b1)
        button_sizer.Add(self.b2)
        button_sizer.Add(self.b3)
        self.sizer.Add(button_sizer)

        self.SetSizer(self.sizer)
        self.SetupScrolling()
        self.SetAutoLayout(1)

    def _create_table(self):
        _table = wx.grid.Grid(self, -1)
        _table.CreateGrid(0, 2)
        for i in range(1723):  # Work normally If I use 1722 rows
            _table.AppendRows()
            _table.SetCellValue(i, 0, str(i))
        return _table

class TestFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY,
                title="Scroll table", size=(640, 480))
        self.fSizer = wx.BoxSizer(wx.VERTICAL)
        self.fSizer.Add(TestPanel(self), 1, wx.EXPAND)
        self.SetSizer(self.fSizer)
        self.Show()

if __name__ == "__main__":
    app = wx.App(False)
    frame = TestFrame()
    app.MainLoop()

enter image description here


推荐阅读