开发者

ListCtrl - wxPython / Python

My question is if we can assign/bind some value to a certain item and hide that value(or if we can do the same thing in another way).

Example: Lets say the columns on ListCtrl are "Name" and "Description":

self.lc = wx.ListCtrl(self, -1, style=wx.LC_REPORT)
self.lc.InsertColumn(0, 'Name')
self.lc.InsertColumn(1, 'Description')

And when I add a item I want them to show the Name parameter and the description:

num_items = self.lc.GetItemCount()
        self.lc.InsertStringItem(num_items, "Randomname")
        self.lc.SetStringItem(num_items, 1, "Some description here")

Now what I want to do is basically assign something to that item that is not shown so I can access later on the app.

So I would like to add something that is not shown on the app but is on the item value like:

hiddendescription = "Somerandomthing"

Still didn't undestand? Well lets say I add a button to add a item with some other TextCtrls to set the parameters and开发者_运维百科 the TextCtrls parameters are:

"Name"

"Description"

"Hiddendescription"

So then the user fills this textctrls out and clicks the button to create the item, and I basically want only to show the Name and Description and hide the "HiddenDescription" but to do it so I can use it later.

Sorry for explaining more than 1 time on this post but I want to make sure you understand what I pretend to do.


Instead of using the ListCtrl as your data structure, you could keep a separate list/dict of objects that contain all the information you want and refresh the ListCtrl from your other data structure.

For example:

class MyObject(object):
    def __init__(self, name, description, hidden_description):
        self.name = name
        self.description = description
        self.hidden_description = hidden_description

Then in your application:

def __init__(self):
    self.my_items = {}
    self.lc = wx.ListCtrl(self, -1, style=wx.LC_REPORT)
    self.lc.InsertColumn(0, 'Name')
    self.lc.InsertColumn(1, 'Description')

def addItemToMyListCtrl(self, name, description, hidden):
    new_item = MyObject(name, description, hidden)
    self.my_items[name] = new_item
    self.lc.Append((new_item.name, new_item.description))

Then when you want to use your additional data you can just look up the correct item in the dictionary and your data will be there.


the wxListCtrl lets you associate arbitrary data with an item, that will not be displayed - read the docs for the following methods:

SetItemData

GetItemData

FindItemData

The wxListItem class also has GetData and SetData methods.


You could always set the width of the hidden column to zero, that might accomplish what you want. I just tried it in a C++ (non-wx) program and it worked fine.


wx.ListCtrl doesn't let you associate a python object with an item like wx.TreeCtrl does with its extremely useful SetPyData() method (and corresponding GetPyData()).

I haven't tried it myself, but there is code here that shows how to create a class to mix in python data with a list. Although I'll admit, it's not clear to me how you're meant to use it.

It also might be possible to directly inherit from wx.ListCtrl, and add the appropriate methods, but I haven't seen any attempts at that anywhere, so it may be harder than I'm thinking.

Alternately you can just use SetItemData() to store an int in each item, and use that int to index a dict (or list, if the items are ordered reliably and consistently) that contains the associated objects. tgray has already shown how to do this, and it's listed at the page I link above as well, so I won't go over it again.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜