(I placed a similar question not too long ago but received no real answer).
I have a simple Python (3.5) data List that I fill up a listbox with. When I read back selected elements from the listbox, while I can make the output look like a Python list, it does not really behave like a list, and I am unable to address elements in the list. It seems as if the list appears as a string of chars.
The question is simply how can one save a list (integers in my case) in a listbox and read back selected elements as a new viable list of the selected elements.
Here is a working demonstration of the problem:
import tkinter as tk
from tkinter import *
root = tk.Tk()
global listbox
global indatalist
indatalist = [[0, 66, 42], [553, 68, 124], [1106, 64, 3], [3321, 78, 8], [3878, 112, 102]]
listbox = tk.Listbox(root,font = 'TkFixedFont',selectmode=EXTENDED)
def Set(): # Populate the listbox:
global listbox
global indatalist
print('indatalist[1][1]=',indatalist[1][1]) # Nicely prints a 2 dim list
listbox.delete(0,'end') # Clear the listbox
# Populate the listbox
for index, inlist in enumerate(indatalist):
listbox.insert(len(indatalist),(indatalist[index][0],indatalist[index][1],indatalist[index][2]))
listbox.pack()
def Get(): # Read back the listbox
global listbox
selecteddata = listbox.selection_get()
## print(selecteddata)
selecteddata = '[[' + (selecteddata.replace(' ',', ').replace('\n','], [') + ']]')
"""Next option is OK, but can only select one single block"""
## startindex = min(listbox.curselection())
## endindex = max(listbox.curselection())
## selecteddata = listbox.get(startindex,endindex)
"""Next looks like a list of items, but acts as a list of chars"""
print('selecteddata=',selecteddata)
print('selecteddata[1][1]=')
print(selecteddata[1][1]) # Error!. Can not read 2 dim list
stepbutton = tk.Button(root, text = "Set List", command = Set)
stepbutton.pack()
readbutton = tk.Button(root, text = "Get List", command = Get)
readbutton.pack()
root.mainloop()