wxpython listctrl 控件学习与问题讨论
最近在学习wxpython ,在学习listctrl控件时,按照API的方法创建了一个列表,并且实现了排序和数据切换。但是程序运行过程中还是有点问题没搞明白:点击表头排序时,有个别列排序紊乱,不知道是什么原因,切换了数据还是不行,初步判定是创建列的问题。不知道哪位大神可以解释解释。
原代码附上,希望可以共同研究,初学者。大神勿喷,多多指点。{:1_893:} {:1_893:} {:1_893:} {:1_893:}
附件是控件的演示视频。
#-- encoding:UTF-8 --
import wx
from wx.lib.mixins.listctrl import ListCtrlAutoWidthMixin,ListRowHighlighter,ColumnSorterMixin
class DataSource:#数据源
def __init__(self,date) -> None:
self.table6_list = date
def GetColumnHeaders(self):
dic = self.table6_list
key_list = []
for i in dic.keys():
key_list.append(i)
return key_list
def GetCount(self):
return len(self.table6_list)
def GetItem(self, index):
value_list1 = []
value_list2 = []
for i in range(len(self.table6_list)):
dic = self.table6_list
for key in dic:
value_list1.append(str(dic))
value_list2.append(value_list1[:])
value_list1 = []
return value_list2
class VirtualListCtrl(wx.ListCtrl,ListCtrlAutoWidthMixin,ColumnSorterMixin,ListRowHighlighter):
"""
A generic virtual listctrl that fetches data from a DataSource.
"""
def __init__(self, parent, *args, **kw):
wx.ListCtrl.__init__(self, parent,pos=(30, 30), size=(600, 400),
style=wx.LC_REPORT|wx.LC_SINGLE_SEL|wx.LC_HRULES|wx.LC_VRULES)
ListCtrlAutoWidthMixin.__init__(self)
ColumnSorterMixin.__init__(self, len(actresses))
ListRowHighlighter.__init__(self,(224,224,0),2)
self.itemDataMap = actresses
self.SetBackgroundColour((225, 218, 255))
self.Refresh(True)
def GetListCtrl(self):
return self
########################################################################################
dict1 = [{'d': 20, 'xl': '04', 'i': 7854, 'w': 785, 62: 12700, 63: 17300, 64: 30500},
{'d': 25, 'xl': '05', 'i': 19175, 'w': 1534, 62: 14300, 63: 22400, 64: 36000},
{'d': 30, 'xl': '06', 'i': 39760, 'w': 2651, 62: 19300, 63: 29000, 64: 42500},
{'d': 35, 'xl': '07', 'i': 73660, 'w': 4209, 62: 25500, 63: 33500, 64: 55000},
{'d': 40, 'xl': '08', 'i': 125660, 'w': 6283, 62: 29000, 63: 42500, 64: 63000},
{'d': 45, 'xl': '09', 'i': 201290, 'w': 8946, 62: 33700, 63: 52500, 64: 73000},
{'d': 50, 'xl': '10', 'i': 306800, 'w': 12272, 62: 35600, 63: 62000, 64: 88000},
{'d': 55, 'xl': '11', 'i': 449180, 'w': 16334, 62: 44300, 63: 71500, 64: 95000},
{'d': 60, 'xl': '12', 'i': 636170, 'w': 21206, 62: 53000, 63: 82000, 64: 107000}]
date= DataSource(dict1)
# date.GetColumnHeaders()
index1 = date.GetCount()
#将原来的数据映射为字典
itemDataMap =actresses= {}
for i in range(index1):
rows = date.GetItem(index=i)
itemDataMap=rows#添加序列组成新字典
# print(itemDataMap)
##########################################################################################################################################
#################################################################################################
#################################################################################################
class HelloFrame(wx.Frame):
"""
A Frame that says Hello World
"""
def __init__(self, *args, **kw):
# ensure the parent's __init__ is called
super(HelloFrame, self).__init__(*args, **kw)
self.SetSize(800,600)
# create a panel in the frame
global pnl
pnl = wx.Panel(self,size=(800,600))
# create a menu bar
self.makeMenuBar()
# and a status bar
self.CreateStatusBar()
self.SetStatusText("Welcome to wxPython!")
##########################################################################################################
self.list = VirtualListCtrl(pnl)
# 创建列表头
# global dic
dic = dict1
#创建列
key_list = []
for i in dic.keys():
key_list.append(i)
for i in range(len(key_list)):
self.list.InsertColumn(i, str(key_list))
items = actresses.items()
self.list.resizeLastColumn(0)#最后一行设置为0
idx = 0
for key, data in items:
index = self.list.InsertItem(idx, data)
for i in range(len(dic)):
self.list.SetItem(index,i, data)
self.list.SetItemData(index, key)
idx += 1
def makeMenuBar(self):
fileMenu = wx.Menu()
helloItem = fileMenu.Append(-1, "&Hello...\tCtrl-H",
"Help string shown in status bar for this menu item")
fileMenu.AppendSeparator()
exitItem = fileMenu.Append(wx.ID_EXIT)
helpMenu = wx.Menu()
aboutItem = helpMenu.Append(wx.ID_ABOUT)
menuBar = wx.MenuBar()
menuBar.Append(fileMenu, "&File")
menuBar.Append(helpMenu, "&Help")
self.SetMenuBar(menuBar)
self.Bind(wx.EVT_MENU, self.OnHello, helloItem)
self.Bind(wx.EVT_MENU, self.OnExit,exitItem)
self.Bind(wx.EVT_MENU, self.OnAbout, aboutItem)
def OnExit(self, event):
self.Close(True)
def OnHello(self, event):
self.list.Destroy()
def OnAbout(self, event):
# self.list.Destroy()
dict2 = [{'d': 20, 'xl': '04', 'i': 7854,62: 12700, 63: 17300},
{'d': 1, 'xl': '05', 'i': 19175, 62: 14300, 63: 22400},
{'d':1, 'xl': '06', 'i': 39760,62: 19300, 63: 29000},
{'d': 1, 'xl': '07', 'i': 73660,62: 25500, 63: 33500},
{'d': 1, 'xl': '08', 'i': 125660,62: 29000, 63: 42500},
{'d': 1, 'xl': '09', 'i': 201290,62: 33700, 63: 52500},
{'d': 1, 'xl': '10', 'i': 306800,62: 35600, 63: 62000},
{'d': 1, 'xl': '11', 'i': 449180, 62: 44300, 63: 71500},
{'d': 1, 'xl': '12', 'i': 636170,62: 53000, 63: 82000}]
date2= DataSource(dict2)
# date.GetColumnHeaders()
index2 = date2.GetCount()
#将原来的数据映射为字典
itemDataMap =actresses= {}
for i in range(index2):
rows = date2.GetItem(index=i)
itemDataMap=rows#添加序列组成新字典
print(itemDataMap)
self.list.DeleteAllItems()
self.list.DeleteAllColumns()
# 创建列表头
dic = dict2
#创建列
key_list = []
for i in dic.keys():
key_list.append(i)
for i in range(len(key_list)):
self.list.InsertColumn(i, str(key_list))
items = actresses.items()
self.list.resizeLastColumn(0)#最后一行设置为0
idx = 0
for key, data in items:
index = self.list.InsertItem(idx, data)
for i in range(len(dic)):
self.list.SetItem(index,i, data)
self.list.SetItemData(index, key)
idx += 1
if __name__ == '__main__':
app = wx.App()
frm = HelloFrame(None, title='Hello World 2')
frm.Show()
app.MainLoop()
页:
[1]