wxPython, wxGrid: remove the white part on the bottom of the grid?

1.1k Views Asked by At

I'm building a small wxPython app which role is to display Clearquest information in a wxGrid. The grid's parent is a panel, the panel parent is a notebook. Everytime I click on a button, I create a new page, get my information and display them. So far so good.

My issue is that, when I scroll to the bottom of the grid, there is an horizontal blank space that I would like to get rid off. Could anyone tell me if there is a way to remove it?

Here is a picture to help understand my issue. I would like to get rid of the white space in the red rectangle.

edit: As asked below, here are some precisions about how the sizers are defined: As you can see, the window is divided in two parts: - the left part is dedicated to the notebook. Each tab of the notebook is a panel in which a wx.Grid is used to display my data - the right part is dedicated to the buttons (it's a Static Box) that triggers the data retrieval.

First, I declare the frame in which I set the panel that will contain the notebook and the Static Box for the buttons (I show the code for only one button, the others don't matter):

class MainFrame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, parent=None, title='ClearQuest Python Edition')
        self.queryHandler = CQHandler.QueryHandler()
        panel = wx.Panel(self)
        queryButtonsBox = wx.StaticBox(panel, -1, 'Run queries')
        queryButtonsSizer = wx.StaticBoxSizer(queryButtonsBox, wx.VERTICAL)
        queryButtonDAS = wx.Button(panel, label="DAS", name="1")
        queryButtonsSizer.Add(queryButtonDAS, flag=wx.TOP | wx.EXPAND, border = 5)
        queryButtonDAS.Bind(wx.EVT_BUTTON, self.QueryButtonClick)
        self.noteBook = MyNoteBook(panel)
        HBox = wx.BoxSizer(wx.HORIZONTAL)
        HBox.Add(self.noteBook, 1, wx.EXPAND, 0)
        HBox.Add(queryButtonsSizer, 0, wx.ALL, 5)
        panel.SetSizer(HBox)

And here is how the notebook and each tab of the notebook are declared and called:

class MyNoteBook(wx.Notebook):

    def __init__(self, parent):
        wx.Notebook.__init__(self, parent, id=wx.ID_ANY, style=wx.BK_DEFAULT)

    def AddQueryPanel(self, title):
        newTab = TabPanel(self)
        self.AddPage(newTab, title, True)
        self.CurrentPage.dataGrid.ForceRefresh()


class TabPanel(wx.Panel):

        def __init__(self, parent):
            wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
            self.dataGrid = MainGrid(self)
            horizontalSizer = wx.BoxSizer(wx.HORIZONTAL)
            horizontalSizer.Add(self.dataGrid, 1, wx.EXPAND | wx.ALL, 0)
            self.SetSizer(horizontalSizer)

And finally, a small sample of my MainGrid, if it can help:

class MainGrid(GridLib.Grid):

    def __init__(self, parent):
        self.queryHandler = CQHandler.QueryHandler()
        self.GetQueryInfo()
        self.style = GUI.Style.StyleHandler()
        GridLib.Grid.__init__(self, parent)
        self.SetGridLineColour("black")
        self.SetRowLabelSize(25)
        self.SetDefaultRowSize(20)
        self.CreateGrid(self.numberOfRows, self.NumberOfColumns)
        self.ConfigureEvents()
        for i in range(self.NumberOfColumns):
            self.SetColLabelValue(i, self.Headers[i])
        self.EnableEditing(False)
        self.CRList = self.queryHandler.GetCRList()
        self.DisplayCR()
2

There are 2 best solutions below

3
On BEST ANSWER

How are the sizers applied? need some details about the code.

1
On

For anyone else that is trying to do this I came up with a solution. This is straight out of my program so you can adjust it how you would like. The sizers keep the panel from being bigger than the grid itself. I used the sunken border to hide the lines you get from hiding the labels.

Any question?

def add_grid(self, num_rows, num_cols, labels = None, grid_labels = None, 
             panel_colour = MainWindow.right_panel_color):

    grid_panel = BuildPanel(self, BackColour = None, Border = wx.SUNKEN_BORDER)

    grid_to_add = wx.grid.Grid(grid_panel)

    if grid_labels is None:
        grid_to_add.SetColLabelSize(0)
        grid_to_add.SetRowLabelSize(0)

    grid_to_add.CreateGrid(num_rows, num_cols)

    total_grid_size = 350
    min_size = total_grid_size/num_cols

    for cols in range(0, num_cols):

        grid_to_add.SetColMinimalWidth(cols, min_size)
        grid_to_add.SetColSize(cols, min_size)

    grid_to_add.SetMargins(0 - wx.SYS_VSCROLL_X, 0 - wx.SYS_HSCROLL_Y)
    grid_to_add.ForceRefresh()

    grid_to_add.SetDefaultCellBackgroundColour(panel_colour)
    for row in range(0, num_rows):
        for col in range(0, num_cols):
            grid_to_add.SetCellBackgroundColour(row, col, wx.WHITE) #None)

    vertical_sizer = wx.BoxSizer(wx.VERTICAL)
    vertical_sizer.Add(grid_to_add, 1, wx.ALL, 0)
    grid_panel.SetSizerAndFit(vertical_sizer)


    h_sizer = wx.BoxSizer(wx.HORIZONTAL)
    h_sizer.AddStretchSpacer(1)
    h_sizer.Add(grid_panel, 0)
    h_sizer.AddStretchSpacer(1)

    return [h_sizer, grid_to_add]