0

(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()
1
  • are you asking how to store a list in each element or how to show each element of a list as an item in a listbox? Commented Oct 30, 2018 at 11:53

1 Answer 1

1

selection_get is not designed to get back the data that is in the listbox. Instead, it is designed to get the X selection, which is by definition a string.

If you want to get the data out of the listbox in the same format that went into the listbox, use the get method.

Here's a simple example of a list comprehension:

selecteddata = [listbox.get(index) for index in listbox.curselection()]
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but your answer does not seem to be accurate (or I am missing something): As can be seen in my example, I can get back any block from the entire list by listbox.curselection() for a selected section. What I need is to select several random items from the listbox and present as a new list. I can get the itmes as requested, but I do not know how to make the selected items a list. Please, try the code also with the listbox.curselection() option.
@samtal: my apologies. I misunderstood the question. I've rewritten my answer to be more accurate.
Thanks Bryan. Solved the problem simple and easy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.