wxpython listctrl 属性_wxPython4.0中ListCtrl入门

wxpython是Python常用的GUI工具之一,最近的最新版本的wxpython4.0,配合Python3.6,下面介绍一下新版本中ListCtrl的使用方法。

首先贴出一个demo:

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()1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

第一步:添加ListCtrl

在frame中添加ListCtrl控件,这时需要用到ListCtrl的构造函数,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)-该列表名字1

2

3

4

5

6

7

8

9

10

11

12

13

第二步:

设置列标题,是通过InsertColumn()函数来实现,一共有两个参数,第一个参数为列索引,第二个参数为列标题名称。

第三步:

插入数据。在最新版本中,插入数据不能使用InsertStringItem()方法,这个方法被淘汰了,官方建议使用InsertItem()方法。

InsertItem()有一下几种重载,该函数返回值为在ListCtrl插入的位置索引:

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位置插入图片和文本内容,组合使用,字段类型参考上面的。1

2

3

4

5

6

7

8

9

10

11

12

第四步:

扩展列。用InsertItem()插入一个item时,该item仅仅有一列,需要扩展多列时,可以调用ListCtrl的SetItem()方法。这个函数有两种实现:

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关联的图片列表中特定的图片。1

2

3

4

5

6

然后大家在结合例子,便能很好的理解。

下载完之后,点击main.html就会弹出文档主界面,文档主界面中wx.core是关于wx对象的详细介绍,包括各种控件以及相应的方法。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值