我试图创建 ListCtrl 派生类来添加 2 个函数set_data
,_set_column_width
这里是代码:
class ListReport(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
def __init__(self, parent=None, columns=['DEFAULT'], size=wx.DefaultSize,
pos=wx.DefaultPosition, style=wx.LC_REPORT|wx.LC_VRULES):
wx.ListCtrl.__init__(self, parent, wx.ID_ANY, pos, size, style)
listmix.ListCtrlAutoWidthMixin.__init__(self)
self.columns = columns
self.column_index = len(columns)
self.InsertColumn(0, 'No.')
for enum, column in enumerate(columns):
enum += 1
self.InsertColumn(enum, column)
def set_data(self, data):
# Change all data into str
values = [map(str, value) for value in data]
for enum, value in enumerate(values):
enum += 1
first = self.InsertStringItem(sys.maxint, str(enum))
for x in range(self.column_index):
if value[x] == None:
self.SetStringItem(first, x+1, '-')
else:
self.SetStringItem(first, x+1, value[x])
# Give ListCtrl some colors
if first % 2:
self.SetItemBackgroundColour(first, 'E8E8E8')
else:
self.SetItemBackgroundColour(first, '#FBFF94')
self._set_column_width(data)
def _set_column_width(self, data=None):
"""Automatically resize column based on data or column header
if column header longer than data using wx.LIST_AUTOSIZE_USEHEADER
else using data instead.
"""
for x in range(self.column_index):
longest = max([len(str(row[x])) for row in data])
if len(self.columns[x]) + 3 > longest:
self.SetColumnWidth(x+1, wx.LIST_AUTOSIZE_USEHEADER)
else:
self.SetColumnWidth(x+1, wx.LIST_AUTOSIZE)
self.SetColumnWidth(0, 37)
self.resizeLastColumn(0)
然后,我使用上面的类来创建另一个类TextEditMixin
class ListReportEdit(ListReport, listmix.TextEditMixin):
def __init__(self, parent=None, columns=['DEFAULT'], size=wx.DefaultSize,
pos=wx.DefaultPosition, style=wx.LC_REPORT|wx.LC_VRULES):
ListReport(parent, columns, size, pos, style)
listmix.TextEditMixin.__init__(self)
但是,我在尝试使用它时遇到了这个错误
Traceback (most recent call last):
File "wxform_sandbox.py", line 263, in <module>
a = Sandbox()
File "wxform_sandbox.py", line 27, in __init__
lr = ListReportEdit(panel, columns, size=(400, 300))
File "wxform_sandbox.py", line 145, in __init__
listmix.TextEditMixin.__init__(self)
File "C:\Program Files\Python27\lib\site-packages\wx-2.8-msw-unicode\wx\lib\mixins\listctrl.py", line 451, in __init__
self.make_editor()
File "C:\Program Files\Python27\lib\site-packages\wx-2.8-msw-unicode\wx\lib\mixins\listctrl.py", line 466, in make_editor
editor = wx.TextCtrl(self, -1, style=style)
File "C:\Program Files\Python27\lib\site-packages\wx-2.8-msw-unicode\wx\_controls.py", line 1736, in __init__
_controls_.TextCtrl_swiginit(self,_controls_.new_TextCtrl(*args, **kwargs))
TypeError: in method 'new_TextCtrl', expected argument 1 of type 'wxWindow *'
但是,当我使用它wx.ListCtrl
作为基类ListReportEdit
而不是ListReport
它完美地工作时,我并不清楚python继承是如何工作的。
提前致谢