tidy
tidy

Reputation: 5057

wxpython how to recreate grid?

I want to recreate a grid. For example: old grid is 4x4, I want change it to 5x5. Here is my code:

import wx
import wx.xrc
import wx.grid

class MyFrame2(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, id=wx.ID_ANY, title=wx.EmptyString, pos=wx.DefaultPosition,
                          size=wx.Size(500, 300), style=wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL)

        bSizer4 = wx.BoxSizer(wx.VERTICAL)
        self.m_grid2 = wx.grid.Grid(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, 0)

        # Grid
        self.m_grid2.CreateGrid(4, 4)
        bSizer4.Add(self.m_grid2, 0, wx.ALL, 5)

        self.m_button3 = wx.Button(self, wx.ID_ANY, u"MyButton", wx.DefaultPosition, wx.DefaultSize, 0)
        bSizer4.Add(self.m_button3, 0, wx.ALL, 5)
        self.m_button3.Bind(wx.EVT_BUTTON, self.OnClick)

        self.SetSizer(bSizer4)
        self.Layout()

    def OnClick(self, event):
        self.m_grid2.CreateGrid(5, 5)
        self.Layout()


app = wx.App()
frame = MyFrame2(None)
frame.Show(True)
app.MainLoop()

It raise an error when I run that:

File "C:\Python27\lib\site-packages\wx-3.0-msw\wx\grid.py", line 1221, in CreateGrid
    return _grid.Grid_CreateGrid(*args, **kwargs)
wx._core.PyAssertionError: C++ assertion "!m_created" failed at ..\..\src\generic\grid.cpp(2325) in wxGrid::CreateGrid(): wxGrid::CreateGrid or wxGrid::SetTable called more than once

It seems I can't recreate this grid again. How can do this job?

Upvotes: 2

Views: 1912

Answers (2)

Mike Driscoll
Mike Driscoll

Reputation: 33071

If you don't want to Append the rows and columns, then you'll just have to recreate the grid itself. Here's a fairly simple demo that demonstrates how to do that:

import wx
import wx.grid as gridlib


########################################################################
class MyGrid(gridlib.Grid):

    #----------------------------------------------------------------------
    def __init__(self, parent, rows, cols):
        gridlib.Grid.__init__(self, parent)
        self.CreateGrid(rows, cols)


########################################################################
class MyPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)
        self.grid_created = False

        row_sizer = wx.BoxSizer(wx.HORIZONTAL)
        col_sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.main_sizer = wx.BoxSizer(wx.VERTICAL)

        rows_lbl = wx.StaticText(self, label="Rows", size=(30, -1))
        row_sizer.Add(rows_lbl, 0, wx.ALL|wx.CENTER, 5)
        self.rows = wx.TextCtrl(self)
        row_sizer.Add(self.rows, 0, wx.ALL|wx.EXPAND, 5)

        cols_lbl = wx.StaticText(self, label="Cols", size=(30, -1))
        col_sizer.Add(cols_lbl, 0, wx.ALL|wx.CENTER, 5)
        self.cols = wx.TextCtrl(self)
        col_sizer.Add(self.cols, 0, wx.ALL|wx.EXPAND, 5)

        grid_btn = wx.Button(self, label="Create Grid")
        grid_btn.Bind(wx.EVT_BUTTON, self.create_grid)

        self.main_sizer.Add(row_sizer, 0, wx.EXPAND)
        self.main_sizer.Add(col_sizer, 0, wx.EXPAND)
        self.main_sizer.Add(grid_btn, 0, wx.ALL|wx.CENTER, 5)

        self.SetSizer(self.main_sizer)

    #----------------------------------------------------------------------
    def create_grid(self, event):
        """"""
        rows = int( self.rows.GetValue() )
        cols = int( self.cols.GetValue() )

        if self.grid_created:
            for child in self.main_sizer.GetChildren():
                widget = child.GetWindow()
                if isinstance(widget, gridlib.Grid):
                    self.main_sizer.Remove(widget)

        grid = MyGrid(self, rows, cols)
        self.main_sizer.Add(grid, 0, wx.ALL, 5)
        self.grid_created = True

        self.main_sizer.Layout()


########################################################################
class MyFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="grids", size=(800, 600))
        panel = MyPanel(self)
        self.Show()

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

Upvotes: 2

Lokla
Lokla

Reputation: 386

You may only call the CreateGrid function once. if you want to change the size, you need to use the functions AppendCols, AppendRows, DeleteCols or DeleteRows.

def OnClick(self, event):
    self.m_grid2.AppendCols(1)
    self.m_grid2.AppendRows(1)
    self.Layout()

Lokla

Upvotes: 1

Related Questions