wxPython's wx.grid.Grid() won't come fully into view

Question:

Issue:

I’m experiencing an issue where a function that simply creates a Grid() works when called in one place, but not another place. When it is called from the “other,” non-working place, it does create a very small square in the corner of the window. At this time, I don’t understand why, and I’m hoping someone can help.

Code: (feel free to copy paste this into a text editor and give it a go!)

import wx
import wx.grid as gridlib


class MainFrame(wx.Frame):

    def __init__(self, parent, title):
        super(MainFrame, self).__init__(parent, title=title,
                                        size=(350, 250))
        self.init_ux()
        self.main_grid = None

    def init_ux(self):
        menu_bar = wx.MenuBar()
        file_menu = wx.Menu()

        file_menu.AppendSeparator()
        menu_bar.Append(file_menu, '&File')

        # add open file menu
        open_menu = wx.Menu()
        my_btn = open_menu.Append(wx.ID_ANY, 'button description')

        # append open_menu to the file_menu
        file_menu.Append(wx.ID_OPEN, '&Open', open_menu)
        self.SetMenuBar(menu_bar)
        self.Bind(wx.EVT_MENU, lambda event: self.open_dialog(data="i love string literals."), my_btn)
        self.SetSize((300, 200))
        self.Centre()
        # the create_grid_view() below DOES work when uncommented
        #self.create_grid_view(10, 10)

    def create_grid_view(self, row_count, col_count):
        print("Creating grid view!")
        # set up grid panel
        panel = wx.Panel(self)
        self.main_grid = gridlib.Grid(panel)
        self.main_grid.CreateGrid(row_count, col_count)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.main_grid, 1, wx.EXPAND)
        panel.SetSizer(sizer)

    def open_dialog(self, data):

        # data is being used for populating wildcard, etc

        with wx.FileDialog(self, "Open a file!",
                           style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as fileDialog:

            if fileDialog.ShowModal() == wx.ID_CANCEL:
                return

            file_path = fileDialog.GetPath()
            try:
                # here, I do some fun data "things" with the file_path
                # open it, use other functions, etc.
                # afterwards, I need to create a grid
                self.create_grid_view(10, 10)
                # ^^ This creates a small square instead of a grid.

            except IOError:
                wx.LogError("Cannot open file '%s'." % file_path)


if __name__ == "__main__":
    app = wx.App()

    frame = MainFrame(None, title='Window Title :)')
    frame.Show()

    app.MainLoop()

Expectation:

enter image description here

Actual Results:

enter image description here

Summary:

Why does the create_grid_view() function display a proper grid when called from the init_ux() function, but not the open_dialog() function?

Thanks in advance!

Asked By: Joshua Schlichting

||

Answers:

Where you have panel.SetSizer(sizer) either use:

panel.SetSizerAndFit(sizer)

or use:

panel.SetSizer(sizer)
panel.Fit()
Answered By: Rolf of Saxony