'Unable to retrieve value from tkinter Listbox after closing the main window
I have designed a Tkinter listbox which looks like this:

When I am in the main loop I can select the options 'A', 'B', etc and print it in the console. When I click proceed the window closes. Below is the code:
from tkinter import *
from tkinter import ttk
main = Tk()
main.title("Parameter Selection")
main.geometry("800x300")
main.configure(bg='mint cream')
frame = ttk.Frame(main, padding=(3, 3, 12, 12))
frame.grid(column=0, row=0, sticky=(N, S, E, W))
scrollbar = Scrollbar(main)
person = StringVar()
person.set("A B C D E")
lstbox1 = Listbox(frame, listvariable=person, selectmode=MULTIPLE, width=15, height=8)
lstbox1.grid(column=0, row=2, columnspan=2)
lstbox1.configure(background="skyblue4", foreground="white", font=('Aerial 13'))
def select1():
reslist = list()
seleccion = lstbox1.curselection()
for i in seleccion:
entry = lstbox1.get(i)
reslist.append(entry)
for val in reslist:
print(val)
return reslist
def quit():
main.destroy()
btn1 = ttk.Button(frame, text="Select Person Name", command=select1)
btn1.grid(column=1, row=3)
destroy_button = ttk.Button(frame, text="Proceed", command=quit)
destroy_button.grid(column=10, row=13)
main.mainloop()
Now I try to retrieve these entry value in another file. Code below:
import main
person = main.select1()
print(person)
Ideally, in combobox or some other Tkinter framework, I am able to retrieve value like this because quit() function in the first code is destroying the window. But in this one I am getting this error from the listbox implementation file:
return self._getints(self.tk.call(self._w, 'curselection')) or ()**
_tkinter.TclError: invalid command name ".!frame.!listbox"
Solution 1:[1]
You can't retrieve the Listbox values after calling quit() because it call main.destroy() which deletes all the children attached to the Tk instance. To workaround that you could make your Select Person Name button call a function that saves a copy of the items selected in the Listbox into a module level global variable, and then change your select1() function return the contents of that.
Here's your code with those changes:
from tkinter import *
from tkinter import ttk
main = Tk()
main.title("Parameter Selection")
main.geometry("800x300")
main.configure(bg='mint cream')
frame = ttk.Frame(main, padding=(3, 3, 12, 12))
frame.grid(column=0, row=0, sticky=(N, S, E, W))
scrollbar = Scrollbar(main)
person = StringVar()
person.set("A B C D E")
lstbox1 = Listbox(frame, listvariable=person, selectmode=MULTIPLE, width=15, height=8)
lstbox1.grid(column=0, row=2, columnspan=2)
lstbox1.configure(background="skyblue4", foreground="white", font=('Aerial 13'))
reslist = list()
def save_selection():
reslist.clear()
selections = lstbox1.curselection()
for i in selections:
entry = lstbox1.get(i)
reslist.append(entry)
for val in reslist:
print(val)
def select1():
return reslist
def quit():
main.destroy()
btn1 = ttk.Button(frame, text="Select Person Name", command=save_selection)
btn1.grid(column=1, row=3)
destroy_button = ttk.Button(frame, text="Proceed", command=quit)
destroy_button.grid(column=10, row=13)
main.mainloop()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | martineau |
