Python Forum

Full Version: Listbox data
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
On raspberry-pi, Python 3.5, I am trying the use of tkinter Listbox.
I noticed that when filling up a Listbox with a pure Python List, while the expected outcome is a list, it is actually tuple.
This means that I can not use the data as expected, and may need to make additional format conversion.
There is further a difference in the format, depending on the command used to extract the list data.
When using the selection-get, the expected numerical type becomes string (or char).

Here is a small test program I have built that demonstrates the options I have tried.
Is there a better way to get a selection of numerical data ?

import tkinter as tk
from tkinter import *
from tkinter.font import Font

root = tk.Tk()

global listbox
global indatalist

indatalist = [[0, 66, 42], [553, 68, 124], [1106, 64, 3]]
listbox = tk.Listbox(root,font = 'TkFixedFont',selectmode=EXTENDED)

def Set():  # Populate the listbox:
    global listbox
    global indatalist
    print('In List=',indatalist)
## Try this option 1 (comment / uncomment to try):
    for index, inlist in enumerate(indatalist):
## Option 2 (comment / uncomment to try)
##  listbox.insert(0,indatalist)

def Get():  # Read back the list box
    global listbox
## Option 1
    outdatalist = listbox.get(0,'end')
    print(outdatalist[0][0] + outdatalist[0][1] + outdatalist[0][2]) #Use to test data type
## Option 2
    outdatalist = listbox.selection_get()
    print('Get Selection='), print(outdatalist)
    print(outdatalist[0][0] + outdatalist[0][1] + outdatalist[0][2]) #Use to test data type
stepbutton = tk.Button(root, text = "Set", command = Set)

readbutton = tk.Button(root, text = "Get", command = Get)

your 'indatalist' is a list of lists. Then when you load the listbox, you load inlist which is a list from indatalist
since each of these entries contain 3 elements, each row of the listbox will contain 3 numbers.
If you want to be able to access each individually, perhaps a treeview would be better (this is in tkinter.ttk), or split each entry of inlist and insert on separate lines of the listbox.
Yes, true, my list is a list of list, or two-dimensional array.
The fact is that when I use listbox.get(start,end) it runs perfectly as alist (or array).
only in listbox.selection_get I get the error.
I can work-around to modify the outcome to look like a list by:

outdatalist = '[[' + listbox.selection_get().replace('\n','],[').replace(' ',',') + ']]'

which will make the output look like a list: Selection_Get= [[0,66,42],[553,68,124],[1106,64,3]], but it still does not behave like a list and does not recognize the records as integers.
listbox.get is intended to capture all data, while listbox.selection_get is for capturing only selected records. They should have the same output.
To me, the fact that listbox.get is ok while listbox.selection_get is in error, looks like a bug.
I will try the treeview and see if it works for me.
Another (admittedly a hack) possibility is to create a parallel list (containing display string and index) for display which also contains index to actual data.
Then when row is selected, fetching data through index.
My hack is by using the listbox.curselection() to find the lower and upper list selection, and then use the working listbox.get(startindex,endindex).
This is not a full solution, as it does not enable multiple selected sections, but works well for single section.
I will try to search for another solution possibly by finding the differences between listbox.selection_get and listbox.get in the libraries (If I can find them?!).
Here is what I do:

startindex = min(listbox.curselection())
endindex = max(listbox.curselection())
datalist = listbox.get(startindex,endindex)
print (datalist[0][0] + datalist[0][1] + datalist[0][2])
you might also consider treeview. I have use it with both single and multiple columns and have example here: