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 ?
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()
