Search code examples
pythonwxpython

Grid shows up for split second and then immediately disappears


I'm making a small project with wxpython and I'm having some trouble with wx.grid.Grid. The grid is nested in a sizer which is nested in another sizer (here's a photo). However, when it's like that, the grid shows up for a split second and disappears. It didn't disappear when it was just inside one sizer.

I've tried changing the sizer type and various properties of the sizer and the grid with no avail. I've also tried changing parts of code but realized that it has nothing to do with it since the grid doesn't even show inside wxGlade.

Here's a part of .py generated by wxGlade:

class frameRozvrh(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: frameRozvrh.__init__
        kwds["style"] = kwds.get("style", 0) | wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.SetSize((640, 480))
        self.statusbar = self.CreateStatusBar(1)
        self.buttonPrev = wx.Button(self, wx.ID_ANY, "<<")
        self.dateWeek = wx.adv.DatePickerCtrl(self, wx.ID_ANY, style=wx.adv.DP_DEFAULT)
        self.buttonNext = wx.Button(self, wx.ID_ANY, ">>")
        self.gridRozvrh = wx.grid.Grid(self, wx.ID_ANY, size=(1, 1))

        self.__set_properties()
        self.__do_layout()
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: frameRozvrh.__set_properties
        self.SetTitle("Rozvrh hodin")
        self.statusbar.SetStatusWidths([-1])

        # statusbar fields
        statusbar_fields = [u"Aktuální týden"]
        for i in range(len(statusbar_fields)):
            self.statusbar.SetStatusText(statusbar_fields[i], i)
        self.dateWeek.Enable(False)
        self.gridRozvrh.CreateGrid(0, 0)
        self.gridRozvrh.EnableEditing(0)
        self.gridRozvrh.EnableDragColSize(0)
        self.gridRozvrh.EnableDragRowSize(0)
        self.gridRozvrh.EnableDragGridSize(0)
        self.gridRozvrh.SetFocus()
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: frameRozvrh.__do_layout
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer_2 = wx.BoxSizer(wx.VERTICAL)
        sizer_1 = wx.GridBagSizer(0, 0)
        sizer_1.Add(self.buttonPrev, (0, 0), (1, 1), wx.ALIGN_CENTER | wx.ALL | wx.EXPAND, 5)
        sizer_1.Add(self.dateWeek, (0, 1), (1, 1), wx.ALIGN_CENTER | wx.ALL | wx.EXPAND, 5)
        sizer_1.Add(self.buttonNext, (0, 2), (1, 1), wx.ALIGN_CENTER | wx.ALL | wx.EXPAND, 5)
        sizer_1.AddGrowableCol(0)
        sizer_1.AddGrowableCol(1)
        sizer_1.AddGrowableCol(2)
        sizer.Add(sizer_1, 0, wx.ALIGN_CENTER | wx.ALL | wx.EXPAND, 10)
        sizer_2.Add(self.gridRozvrh, 0, wx.ALL | wx.EXPAND, 5)
        sizer.Add(sizer_2, 0, wx.ALIGN_CENTER | wx.ALL | wx.EXPAND, 10)
        self.SetSizer(sizer)
        self.Layout()

I'd like the grid to stay visible, not disappear.


Solution

  • There are two problems in your code

    Problem 1 is in this line:

    self.gridRozvrh = wx.grid.Grid(self, wx.ID_ANY, size=(1, 1))
    

    With this line your creating a grid with 1 pixel wide and 1 pixel height. Change it to:

    self.gridRozvrh = wx.grid.Grid(self, wx.ID_ANY, size=(100, 100))
    

    or any other size.

    Problem 2 is in this line:

    self.gridRozvrh.CreateGrid(0, 0)
    

    With this line you are creating a grid with 0 rows and 0 columns. Change this to:

    self.gridRozvrh.CreateGrid(10, 10)
    

    or any other numbers as needed.