wxpython是Python常用的GUI工具之一,最近的最新版本的wxpython4.0,配合Python3.6,下面介绍一下新版本中ListCtrl的使用方法。 import wxbookdata = { 1 : ('钢铁是怎样炼成的练成的', '2017-05-31'), 2 : ('秦腔', '2017-04-12'), 3 : ('西游记', '1987-08-12')}class LibrarySys(wx.Frame): def __init__(self, parent, title): '''初始化系统总体布局,包括各种控件''' #生成一个宽为400,高为600的frame框 wx.Frame.__init__(self, parent, title=title, size=(400, 400)) #生成一个列表 self.list = wx.ListCtrl(self, -1, style = wx.LC_REPORT) self.list.InsertColumn(0, 'ID') self.list.InsertColumn(1, '书名') self.list.InsertColumn(2, '添加日期') items = bookdata.items() #将字典数据转化为序列 for key, data in items: #插入一个item,参数1为在什么地方插入,参数二为这个item的文本内容,刚开始item默认仅有一列 index = self.list.InsertItem(self.list.GetItemCount(), str(key)); self.list.SetItem(index, 1, data[0]) #添加一列,并设置文本为data[0] self.list.SetItem(index, 2, data[1]) #再添加一列,设置文本为data[1] self.list.SetColumnWidth(0, 60) #设置每一列的宽度 self.list.SetColumnWidth(1,230) self.list.SetColumnWidth(2, 90) self.Show()#类似于c中的main函数,但被其他模块导入时,__name__值不是'__main__'if __name__ == '__main__': app = wx.App() frame = LibrarySys(None, 'library-system') app.MainLoop()
第一步:添加ListCtrl __init__(self) 默认构造函数__init__(self, parent, id=ID_ANY, pos=DefaultPosition, size=DefaultSize, style=LC_ICON, validator=DefaultValidator, name=ListCtrlNamesStr) 其中参数解释为: parent(wx.Window)-父容器,绝对不能为None id(wx.WindowID)-该列表窗口ID,默认为ID_ANY,一般不用设置 pos(wx.Point)-该列表中的位置,用wx中的Point类来初始化 size(wx.Size)-该列表大小,用wx.Size类来初始化 style(long)-该列表风格,请参考wx.ListCtrl validator(wx.Validator)-窗口验证器,一般不用设置 name(String)-该列表名字
第二步: 第三步: InsertItem(self, info) 参数info是一个ListItem类的对象,表示想ListCtrl中插入一个ListItem对象,关于ListItem对象信息请参考相应文档。(在博客后面 提供相应文档下载链接)。InsertItem(self, index, label)后面动态的添加列。InsertItem(self, index, imageIndex) index表示在ListCtrl中插入的位置,为long类型;imageIndex为与ListCtrl关联的图片链表索引,表示要插入哪张图片,为int类型。InsertItem(self, index, label, imageIndex) 表示在index位置插入图片和文本内容,组合使用,字段类型参考上面的。
第四步: SetItem(self, info) info为ListItem对象,用ListItem内容替换ListCtrl中的选中列。SetItem(self, index, columm, label, imageId=-1) index表示要操作的item,为long类型;column为要设置的列,可用来扩展列,为int类型;label为要设置的文本内容,为string类型; imageId为要插入的图片的Id,该id用来指向与ListCtrl关联的图片列表中特定的图片。
然后大家在结合例子,便能很好的理解。 wxpython 4.0.0a2的API文档下载地址为:https:///Phoenix/release-extras/4.0.0a2/wxPython-docs-4.0.0a2.tar.gz 下载完之后,点击main.html就会弹出文档主界面,文档主界面中wx.core是关于wx对象的详细介绍,包括各种控件以及相应的方法。 |
|
来自: 我本无我O > 《Python 知识》