wxPython ListCtrlで表をソートしてみる
wxPythonでListCtrlを使って表を表示して、カラムのタイトルをクリックするとソートしてくれるのをやってみた。
前にやったように、wx.ListCtrlを継承したMyTableクラスを作るが、コンストラクタで
wx.LC_VIRTUALを指定してバーチャルで表を作るようにする。
SetItemCount()メソッドをコールすると、OnGetItemTextメソッドにしたがってアイテムを表示する
ソートの結果を表示するときは、DeleteAllItems()メソッドをコールしてから、SetItemCountメソッドをコールしてやるのがミソらしい。
#!/usr/bin/env python
#coding:utf-8
import wx
caption = (u"No",u"都道府県",u"男女計",u"男",u"女")
data = [
(1,u"北海道",5570,2638,2933),
(2,u"青森県",1407,663,744),
(3,u"岩手県",1364,652,712),
(4,u"宮城県",2347,1140,1208),
(5,u"秋田県",1121,527,593),
(6,u"山形県",1198,575,623),
(7,u"福島県",2067,1004,1063),
(8,u"茨城県",2969,1477,1492),
(9,u"栃木県",2014,1001,1013),
(10,u"群馬県",2016,993,1024),
(11,u"埼玉県",7090,3570,3520),
(12,u"千葉県",6098,3047,3051),
(13,u"東京都",12758,6354,6405),
(14,u"神奈川県",8880,4484,4396),
]
class MyTable(wx.ListCtrl):
def __init__(self,parent):
wx.ListCtrl.__init__(self,parent,-1,style = wx.LC_REPORT | wx.LC_VIRTUAL)
self.InsertColumn(0,caption[0],wx.LIST_FORMAT_RIGHT)
self.InsertColumn(1,caption[1])
self.InsertColumn(2,caption[2],wx.LIST_FORMAT_RIGHT)
self.InsertColumn(3,caption[3],wx.LIST_FORMAT_RIGHT)
self.InsertColumn(4,caption[4],wx.LIST_FORMAT_RIGHT)
self.items=data
self.SetItemCount(len(self.items))
self.Bind(wx.EVT_LIST_COL_CLICK,self.Sort)
def OnGetItemText(self,line,col):
return self.items[line][col]
def Sort(self,event):
idx=event.GetColumn()
self.items.sort(lambda x,y: cmp(x[idx],y[idx]))
self.DeleteAllItems()
self.SetItemCount(len(self.items))
class MyWindow(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,"MyTitle",size=(450,300))
MyTable(self)
if __name__=='__main__':
app=wx.PySimpleApp()
frame=MyWindow(parent=None,id=-1)
frame.Show()
app.MainLoop()
実行すると
男女計でソート
表にある”男女計”をクリックして昇順にソートする


