求教如何给listbox的item绑定数据

sky320 2014-12-22 03:30:02
请教下各位大大怎么样能在listbox的每一条item绑定数据呢?其他语言有一个方法可以直接setdata的,python有类似的方法吗。
谢谢!!!

#coding=utf-8
import wx

class ListBoxFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, 'List Box Example', size=(250, 200))
panel = wx.Panel(self, -1)

sampleList = ['zero', 'one', 'two', 'three', 'four', 'five','six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen']
listBox = wx.ListBox(panel, -1, (20, 20), (80, 120), sampleList, wx.LB_SINGLE)
listBox.Bind(wx. EVT_LISTBOX_DCLICK,self.OnListBox)

def OnListBox(self,event):
print "itemdata:"

if __name__ == '__main__':
app = wx.PySimpleApp()
ListBoxFrame().Show()
app.MainLoop()
...全文
184 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
angel_su 2014-12-23
  • 打赏
  • 举报
回复
嗯,原来还有这附加功能,一楼那里说错了...
panghuhu250 2014-12-23
  • 打赏
  • 举报
回复
listbox.SetClientData, GetClientData. 还可以用Append同时加上新的item和data。文档:

In [189]: box2.GetClientData?
Type:        instancemethod
String form: <bound method ListBox.GetClientData of <wx._controls.ListBox; proxy of <Swig Object of type 'wxListBox *' at 0x1cc5150> >>
File:        /usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode/wx/_core.py
Definition:  box2.GetClientData(*args, **kwargs)
Docstring:
GetClientData(self, int n) -> PyObject

Returns the client data associated with the given item, (if any.)

In [190]: box2.SetClientData?
Type:        instancemethod
String form: <bound method ListBox.SetClientData of <wx._controls.ListBox; proxy of <Swig Object of type 'wxListBox *' at 0x1cc5150> >>
File:        /usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode/wx/_core.py
Definition:  box2.SetClientData(*args, **kwargs)
Docstring:
SetClientData(self, int n, PyObject clientData)

Associate the given client data with the item at position n.

In [191]: box2.Append?
Type:        instancemethod
String form: <bound method ListBox.Append of <wx._controls.ListBox; proxy of <Swig Object of type 'wxListBox *' at 0x1cc5150> >>
File:        /usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode/wx/_core.py
Definition:  box2.Append(*args, **kwargs)
Docstring:
Append(self, String item, PyObject clientData=None) -> int

Adds the item to the control, associating the given data with the item
if not None.  The return value is the index of the newly added item
which may be different from the last one if the control is sorted (e.g.
has wx.LB_SORT or wx.CB_SORT style).
angel_su 2014-12-23
  • 打赏
  • 举报
回复
在python下,邦定数据自然是用python对象才会方便,不过需要你要自己去写...
sky320 2014-12-23
  • 打赏
  • 举报
回复
感谢楼上,受教了。

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧