wxpython如何重新创建网格?

时间:2014-10-22 02:52:36

标签: python datagrid wxpython

我想重新创建一个网格。例如:旧网格是4x4,我想将其更改为5x5。 这是我的代码:

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()

运行时会出现错误:

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

似乎我再也无法重新创建这个网格了。怎么做这个工作?

2 个答案:

答案 0 :(得分:2)

如果您不想追加行和列,那么您只需重新创建网格本身。这是一个相当简单的演示,演示了如何做到这一点:

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()

答案 1 :(得分:1)

您只能调用一次CreateGrid函数。如果要更改大小,则需要使用AppendCols,AppendRows,DeleteCols或DeleteRows函数。

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

Lokla