3

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.

4 Answers 4

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Didn't think about this, thanks alot, i'll give a try to this method!
4

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.

2 Comments

Unless I'm missing something, this doesn't let you associate arbitrary data, it lets you associate an integer. This is only useful for 'arbitrary data' if you use tgray's solution.
@Cam: you are right. If you need to store an arbitrary python object you need to put it in a dict and store the key. This page on the wxPython wiki has a mixin class that adds a SetPyData method to do this automatically. It is missing the GetPyData and FindPyData methods, but they are trivial to add.
1

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.

1 Comment

Well, thanks for your help and it would be a good idea if there wasn't a little problem, when I go to resize the Description column it appears to resize either Description column and the hiddendescription column revealing the information... But thanks anyway!
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.