Python Forum
[Tkinter] Listbox data
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Listbox data
#1
Hi,
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
    listbox.delete(0,'end')
    print('In List=',indatalist)
## Try this option 1 (comment / uncomment to try):
    for index, inlist in enumerate(indatalist):
        listbox.insert(len(indatalist),(indatalist[index][0],indatalist[index][1],indatalist[index][2]))
## Option 2 (comment / uncomment to try)
##  listbox.insert(0,indatalist)
    listbox.pack()

def Get():  # Read back the list box
    global listbox
## Option 1
    outdatalist = listbox.get(0,'end')
    print('Get=',outdatalist)
    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)
stepbutton.pack()

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

root.mainloop()
Reply
#2
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.
Reply
#3
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.
Reply
#4
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.
Reply
#5
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])
Reply
#6
you might also consider treeview. I have use it with both single and multiple columns and have example here: https://python-forum.io/Thread-Californi...california
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Button to add data to database and listbox SalsaBeanDip 1 2,807 Dec-06-2020, 10:13 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020