wxPython ListCtrlで表を表示してみる

2011/01/25

wxPythonでListCtrlを使って表を表示してみる。

wx.ListCtrlを継承したMyTableクラスを作り、都道府県別の人口を表示してみた。

#!/usr/bin/env python
#coding:utf-8

import wx

caption = (u"都道府県",u"男女計",u"男",u"女")

data = [
(u"北海道","5,570","2,638","2,933"),
(u"青森県","1,407","663","744"),
(u"岩手県","1,364","652","712"),
(u"宮城県","2,347","1,140","1,208"),
(u"秋田県","1,121","527","593"),
(u"山形県","1,198","575","623"),
(u"福島県","2,067","1,004","1,063"),
(u"茨城県","2,969","1,477","1,492"),
(u"栃木県","2,014","1,001","1,013"),
(u"群馬県","2,016","993","1,024"),
(u"埼玉県","7,090","3,570","3,520"),
(u"千葉県","6,098","3,047","3,051"),
(u"東京都","12,758","6,354","6,405"),
(u"神奈川県","8,880","4,484","4,396"),
]

class MyTable(wx.ListCtrl):
    def __init__(self,parent):
        wx.ListCtrl.__init__(self,parent,-1,style = wx.LC_REPORT | wx.LC_HRULES)

        for col,v in enumerate(caption):
            self.InsertColumn(col,v)

        for line in range(len(data)):
            self.InsertStringItem(line,data[line][0])
            for col in range(1,4):
                self.SetStringItem(line,col,data[line][col])

class MyWindow(wx.Frame):
    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,"MyTitle",size=(300,200))
        MyTable(self)

if __name__=='__main__':
    app=wx.PySimpleApp()
    frame=MyWindow(parent=None,id=-1)
    frame.Show()
    app.MainLoop()

実行すると

表示に使ったデータ

総務省
http://www.stat.go.jp/data/jinsui/2007np/index.htm
都道府県,男女別人口及び人口性比―総人口,日本人人口(平成19年10月1日現在)(エクセル:28KB)

wxPython

Posted by skw