wxPython常用控制元件--wx.Grid,wx.ListBox,wx.ListCtrl,wx.TreeCtrl
阿新 • • 發佈:2019-01-02
(12)表格, wx.grid.Grid,建構函式:
自定義的Grdi控制元件:
xGridTable.py
#coding=utf-8 import wx.grid as grid class StudentInfoGridTable(grid.PyGridTableBase): def __init__(self, datas): grid.PyGridTableBase.__init__(self) self.datas = datas self.colLabels = [u'試卷名', u'制卷人', u'制卷人賬號', u'在需要使用該控制元件的時候,例項化:考試成績', u'測試開始時間', u'測試結束時間', u'測試時長'] self.odd = grid.GridCellAttr() self.odd.SetReadOnly(True) self.odd.SetBackgroundColour('yellow') self.even = grid.GridCellAttr() self.even.SetReadOnly(True) pass def GetAttr(self, row, col, kind): attr = [self.even, self.odd][row % 2] attr.IncRef() return attr def GetNumberRows(self): return len(self.datas) def GetNumberCols(self): return len(self.colLabels) def GetColLabelValue(self, col): return self.colLabels[col] def GetRowLabelValue(self, row): returnstr(row) def GetValue(self, row, col): return self.datas[row][col]
from xGridTable import StudentInfoGridTable
gridDatas = [ [u'大仲馬', u'男', u'WUST', u'中文', u'研三'], [u'牛頓', u'男', u'HUST', u'物理', u'博一'], [u'愛因斯坦', u'男', u'HUST', u'物理', u'研一'], [u'居里夫人', u'女', u'WUST', u'化學', u'研一'], ] self.gridTable = wx.grid.Grid(self, -1, pos=(5, 5), size=(600, 400), style=wx.WANTS_CHARS) self.infoTable = StudentInfoGridTable(gridDatas) self.gridTable.SetTable(self.infoTable, True)效果:
(13)簡單的列表,wx.ListBox,建構函式:
""" __init__(self, Window parent, int id=-1, Point pos=DefaultPosition, Size size=DefaultSize, wxArrayString choices=wxPyEmptyStringArray, long style=0, Validator validator=DefaultValidator, String name=ListBoxNameStr) -> ListBox """
panel = wx.Panel(self) self.textFont = wx.Font(11, wx.DEFAULT, wx.NORMAL, wx.NORMAL, False) listDatas = [u'ListBox是簡單的列表', u'TreeCtrl是樹狀檔案目錄機構', u'ListCtrl複雜的列表', u'RadioButton是單選項', u'CheckBox為多選項', u'StaticText顯示靜態文字',] self.listBox = wx.ListBox(panel, -1, pos=(10, 10), size=(300, 120), choices=listDatas, style=wx.LB_SINGLE) self.listBox.SetFont(self.textFont) self.Bind(wx.EVT_LISTBOX, self.listCtrlSelectFunc, self.listBox)效果:
ListBox的事件監聽:
def listCtrlSelectFunc(self, event): indexSelected = event.GetEventObject().GetSelection() print '選中Item的下標:', indexSelected pass點選效果:
(14)複雜列表,wx.ListCtrl,自定義的列表
xListCtrl.py
#coding=utf-8 import wx class CourseListCtrl(wx.ListCtrl): def __init__(self, parent, subDatas, size, id=-1, pos=(0, 0), style=wx.LC_SMALL_ICON): wx.ListCtrl.__init__(self, parent, id, pos, size, style) self.subDatas = subDatas self.InitUI() pass def InitUI(self): il = wx.ImageList(45, 45, True) logoBmp = wx.Bitmap(r"D:\book_icon.png", wx.BITMAP_TYPE_PNG) il.Add(logoBmp) self.AssignImageList(il, wx.IMAGE_LIST_SMALL) self.ShowListDatas(self.subDatas) pass def ShowListDatas(self, datas): self.subDatas = datas for index in range(len(self.subDatas)): subject = self.subDatas[index] content = u"科目:{0} 科目介紹:{1}".format(subject.get('name', ''), subject.get('intro', '')) self.InsertImageStringItem(index, content, 0) def refreshDataShow(self, newDatas): self.datas = newDatas self.DeleteAllItems() self.ShowListDatas(self.datas) self.Refresh()
在使用ListCtrl的時候,匯入並例項化:
from xListCtrl import CourseListCtrl
courseNames = [u'大學物理', u'計算機技術', u'微積分', u'電力電子'] courseDatas = [] for index in range(len(courseNames)): obj = {} obj['name'] = courseNames[index] obj['intro'] = courseNames[index]+u'的簡介......' courseDatas.append(obj) self.textFont = wx.Font(12, wx.DEFAULT, wx.NORMAL, wx.NORMAL, False) self.courseListCtrl = CourseListCtrl(self, courseDatas, size=(300, 300), pos=(10, 10)) self.courseListCtrl.SetFont(self.textFont) self.courseListCtrl.SetBackgroundColour('#ffffff') self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.courseListSelectFunc, self.courseListCtrl)效果:
ListCtrl事件監聽:
def courseListSelectFunc(self, event): index = event.GetIndex() print u'選中的Item的下標:', index pass
(15)樹狀檔案結構,wx.TreeCtrl,自定義的樹狀圖:
xTreeCtrl.py
#coding=utf-8 import wx import wx.gizmos as gizmos cityNames = { "Beijing": u"北京", "Wuhan": u"武漢", "Shanghai": u"上海", } def GetCityChinexeName(id): return cityNames.get(id, '') class CollegeTreeListCtrl(wx.gizmos.TreeListCtrl): def __init__(self, parent=None, id=-1, pos=(0,0), size=wx.DefaultSize, style=wx.TR_DEFAULT_STYLE|wx.TR_FULL_ROW_HIGHLIGHT): wx.gizmos.TreeListCtrl.__init__(self, parent, id, pos, size, style) self.root = None self.InitUI() pass def InitUI(self): self.il = wx.ImageList(16, 16, True) self.il.Add(wx.ArtProvider_GetBitmap(wx.ART_FOLDER, wx.ART_OTHER, (16, 16))) self.il.Add(wx.ArtProvider_GetBitmap(wx.ART_FILE_OPEN, wx.ART_OTHER, (16, 16))) self.il.Add(wx.ArtProvider_GetBitmap(wx.ART_NORMAL_FILE, wx.ART_OTHER, (16, 16))) self.SetImageList(self.il) self.AddColumn(u'學校名稱') self.AddColumn(u'是否985') self.AddColumn(u'學校型別') pass def ShowItems(self, datas): self.SetColumnWidth(0, 150) self.SetColumnWidth(1, 40) self.SetColumnWidth(2, 47) self.root = self.AddRoot(u'中國大學') self.SetItemText(self.root, "", 1) self.SetItemText(self.root, "", 2) # 填充整個樹 cityIDs = datas.keys() for cityID in cityIDs: child = self.AppendItem(self.root, cityID) lastList = datas.get(cityID, []) childTitle = GetCityChinexeName(cityID)+u" (共"+str(len(lastList))+u"所大學)" self.SetItemText(child, childTitle, 0) self.SetItemText(child, "", 1) self.SetItemText(child, "", 2) self.SetItemImage(child, 0, which=wx.TreeItemIcon_Normal) self.SetItemImage(child, 1, which=wx.TreeItemIcon_Expanded) for index in range(len(lastList)): college = lastList[index] # TreeItemData是每一個ChildItem的唯一標示 # 以便在點選事件中獲得點選項的位置資訊 data = wx.TreeItemData(cityID+"|"+str(index)) last = self.AppendItem(child, str(index), data=data) self.SetItemText(last, college.get('collegeName', ''), 0) self.SetItemText(last, college.get('if_985_type', ''), 1) self.SetItemText(last, college.get('collegeType', ''), 2) self.SetItemImage(last, 0, which=wx.TreeItemIcon_Normal) self.SetItemImage(last, 1, which=wx.TreeItemIcon_Expanded) self.Expand(self.root) pass def refreshDataShow(self, newDatas): if self.root != None: self.DeleteAllItems() if newDatas != None: self.ShowItems(newDatas) def DeleteSubjectItem(self, treeItemId): self.Delete(treeItemId) self.Refresh() pass
在需要顯示資料的地方匯入並例項化:
from xTreeCtrl import CollegeTreeListCtrl
self.treeListCtrl = CollegeTreeListCtrl(parent=self, pos=(-1, 39), size=(300, 300)) self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnTreeListCtrlClickFunc, self.treeListCtrl) self.colleges = { u'Beijing': [ {'collegeName': u'北京大學', 'if_985_type':u'是', 'collegeType':u'綜合類'}, {'collegeName': u'清華大學', 'if_985_type': u'是', 'collegeType': u'理工類'}, {'collegeName': u'北京郵電大學', 'if_985_type': u'否', 'collegeType': u'理工類'}], u'Wuhan':[ {'collegeName': u'武漢大學', 'if_985_type': u'是', 'collegeType': u'綜合類'}, {'collegeName': u'華中科技大學', 'if_985_type': u'是', 'collegeType': u'理工類'} ], u'Shanghai': [ {'collegeName': u'上海交通大學', 'if_985_type': u'是', 'collegeType': u'理工類'}, {'collegeName': u'復旦大學', 'if_985_type': u'是', 'collegeType': u'綜合類'}, {'collegeName': u'同濟大學', 'if_985_type': u'是', 'collegeType': u'綜合類'} ] } # TreeCtrl顯示資料介面 self.treeListCtrl.refreshDataShow(self.colleges)效果:
TreeCtrl的點選事件:
def OnTreeListCtrlClickFunc(self, event): if self.treeListCtrl.GetItemData(event.GetItem()) != None: # 當前選中的TreeItemId物件,方便進行刪除等其他的操作 self.currentTreeItemId = event.GetItem() cityName, collegeIndex = str(self.treeListCtrl.GetItemData(event.GetItem()).GetData()).split("|") collegeObj = self.colleges.get(cityName, [])[int(collegeIndex)] print '點選了:', cityName +u"的"+collegeObj.get('collegeName', '') pass