[求助]wxpython grid模块 新增/删除表格行报错

meteordragon1 2017-01-30 07:32:49
窗口和表格代码是从网上找来的。
自己在按钮动作上加了添加行命令: self.grid.AppendCols(numCols=1)
运行后报错如下:
app = wx.PySimpleApp()
Traceback (most recent call last):
File "D:\email\test\wintest.py", line 52, in OnTest
self.grid.AppendCols(numCols=1)
File "D:\Python27\lib\site-packages\wx-3.0-msw\wx\grid.py", line 1285, in AppendCols
return _grid.Grid_AppendCols(*args, **kwargs)
wx._core.PyAssertionError: C++ assertion "Assert failure" failed at ..\..\src\generic\grid.cpp(1129) in wxGridTableBase:
:AppendCols(): Called grid table class function AppendCols
but your derived table class does not override this function


请问这个报错是怎么回事,怎么排除?
或者在初始化生成表格后怎样重新生成表格~
谢谢回复


代码如下:
import wx, wx.grid

class GridData(wx.grid.PyGridTableBase):
_cols = "a b c".split()
_data = [
"1 2 3".split(),
"4 5 6".split(),
"7 8 9".split()
]
_highlighted = set()

def GetColLabelValue(self, col):
return self._cols[col]

def GetNumberRows(self):
return len(self._data)

def GetNumberCols(self):
return len(self._cols)

def GetValue(self, row, col):
return self._data[row][col]

def SetValue(self, row, col, val):
self._data[row][col] = val

def GetAttr(self, row, col, kind):
attr = wx.grid.GridCellAttr()
attr.SetBackgroundColour(wx.GREEN if row in self._highlighted else wx.WHITE)
return attr

def set_value(self, row, col, val):
self._highlighted.add(row)
self.SetValue(row, col, val)

class Test(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None)

self.data = GridData()
self.grid = wx.grid.Grid(self)
self.grid.SetTable(self.data)

btn = wx.Button(self, label="set a2 to x")
btn.Bind(wx.EVT_BUTTON, self.OnTest)

self.Sizer = wx.BoxSizer(wx.VERTICAL)
self.Sizer.Add(self.grid, 1, wx.EXPAND)
self.Sizer.Add(btn, 0, wx.EXPAND)

def OnTest(self, event):
self.grid.AppendCols(numCols=1)
#self.data.set_value(1, 0, "x")
self.grid.Refresh()


app = wx.PySimpleApp()
app.TopWindow = Test()
app.TopWindow.Show()
app.MainLoop()
...全文
553 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_40366394 2017-12-17
  • 打赏
  • 举报
回复
# -*- coding: cp936 -*- #cording utf8 import wx import wx.lib.buttons as buttons class GenericButtonFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, -1, 'Generic Button Example', size=(500, 350)) panel = wx.Panel(self, -1) sizer = wx.FlexGridSizer(1, 3, 20, 20) b = wx.Button(panel, -1, "A wx.Button") b.SetDefault() sizer.Add(b) b = wx.Button(panel, -1, "non-default wx.Button") sizer.Add(b) sizer.Add((10,10)) b = buttons.GenButton(panel, -1, 'Genric Button')#基本的通用按钮 sizer.Add(b) b = buttons.GenButton(panel, -1, 'disabled Generic')#无效的通用按钮 b.Enable(False) sizer.Add(b) b = buttons.GenButton(panel, -1, 'bigger')#自定义尺寸和颜色的按钮 b.SetFont(wx.Font(20, wx.SWISS, wx.NORMAL, wx.BOLD, False)) b.SetBezelWidth(5) b.SetBackgroundColour("Navy") b.SetForegroundColour("white") b.SetToolTipString("This is a BIG button...") sizer.Add(b) bmp = wx.Image("bitmap.bmp", wx.BITMAP_TYPE_BMP).ConvertToBitmap() b = buttons.GenBitmapButton(panel, -1, bmp)#通用位图按钮 sizer.Add(b) b = buttons.GenBitmapToggleButton(panel, -1, bmp)#通用位图开关按钮 sizer.Add(b) b = buttons.GenBitmapTextButton(panel, -1, bmp, "Bitmapped Text", size=(175, 75))#位图文本按钮 b.SetUseFocusIndicator(False) sizer.Add(b) b = buttons.GenToggleButton(panel, -1, "Toggle Button")#通用开关按钮 sizer.Add(b) panel.SetSizer(sizer) if __name__ == '__main__': app = wx.App() frame = GenericButtonFrame() frame.Show() app.MainLoop()
nieoding 2017-02-06
  • 打赏
  • 举报
回复
GridData再重载一个函数AppendCols

import wx, wx.grid

class GridData(wx.grid.PyGridTableBase):
    _cols = "a b c".split()
    _data = [
        "1 2 3".split(),
        "4 5 6".split(),
        "7 8 9".split()
    ]
    _highlighted = set()

    def GetColLabelValue(self, col):
        return self._cols[col]

    def GetNumberRows(self):
        return len(self._data)

    def GetNumberCols(self):
        return len(self._cols)

    def GetValue(self, row, col):
        return self._data[row][col]

    def SetValue(self, row, col, val):
        self._data[row][col] = val

    def GetAttr(self, row, col, kind):
        attr = wx.grid.GridCellAttr()
        attr.SetBackgroundColour(wx.GREEN if row in self._highlighted else wx.WHITE)
        return attr

    def set_value(self, row, col, val):
        self._highlighted.add(row)
        self.SetValue(row, col, val)
    
    def AppendCols(self,*args, **kwargs):
        self._cols.append('d')
        for data in self._data:
            data.append('')
        msg = wx.grid.GridTableMessage(self,wx.grid.GRIDTABLE_NOTIFY_COLS_APPENDED,1)
        self.GetView().ProcessTableMessage(msg)
    
    
class Test(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)
        self.data = GridData()
        self.grid = wx.grid.Grid(self)
        self.grid.SetTable(self.data)

        btn = wx.Button(self, label="set a2 to x")
        btn.Bind(wx.EVT_BUTTON, self.OnTest)

        self.Sizer = wx.BoxSizer(wx.VERTICAL)
        self.Sizer.Add(self.grid, 1, wx.EXPAND)
        self.Sizer.Add(btn, 0, wx.EXPAND)

    def OnTest(self, event):
        self.grid.AppendCols(numCols=1)
        #self.data.set_value(1, 0, "x")
        self.grid.Refresh()


app = wx.PySimpleApp()
app.TopWindow = Test()
app.TopWindow.Show()
app.MainLoop()
meteordragon1 2017-02-04
  • 打赏
  • 举报
回复
请问要怎样引用?这块不太理解
  • 打赏
  • 举报
回复
按提示错误信息,是那个函数没有重载,应该在你的类里面重载它

37,718

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧