Hidden identifier on tkInter Listbox

I am wondering if it is possible to somehow store the hidden identifier with each listing in the Listbox. The reason for this is because I have a table that contains a unique identifier that is from the database (not displayed to the user, but used to uniquely identify each record). I cache the table in memory and using the dictionary, id

I would like to create a Listbox that allows me to select one of the entries - the displayed text will not be a unique identifier, but a descriptive field (for example, "Name"), which is probably unique, but it does not apply and there is no index on it. For example, if I have:

Id  Name
--  ----
 2  Rod
 5  Jane
15  Freddy

Then, by choosing Jane, I could easily access the id5

My problem is that I cannot find a way to associate a unique id ( 5) with a selection ( Jane) so that I can easily identify the cached record. I know that I can use control variables, but that just gives me a list of all the lines in the list - not what I want. In addition, the index (for example, in the inset) does not appear to be reliable for this purpose.

The only way I was able to do this was to have another dictionary matching the name with the identifier. For a number of reasons, this is not optimal.

Am I missing something? Is there an easier way to do this?

+3
source share
2 answers

, .curselection(), , .

Jane , , .curselection() 1. rowids , rowids[1] 5:

>>> rowids = [2, 5, 15]
>>> rowids[listbox.curselection()]
5

, rowids .

+3
+1

All Articles