i want to return the selected item from a listbox from one module into another. Inside A.py
i call a function from B.py
which creates a small tkinter listbox
. Within this listbox i want to select an item, and return it as a value to A.py
.
I tried several things, i.e.
# inside A.py
import B
filter = B.get_pt_filter()
and
# inside B.py
from tkinter import * # (i know this is not clean, but let's keep it this way for now)
from functools import partial
def button_click(lb):
return lb.get(lb.curselection())
def get_pt_filter():
pt_list = define_pt_list()
# create window
tk_window = Tk()
tk_window.title('Choose')
tk_window.geometry('350x400')
# listbox border
frame_listbox = Frame(master=tk_window, bg='#FFCFC9')
frame_listbox.place(x=5, y=5, width=335, height=360)
# button border
frame_button = Frame(master=tk_window, bg='#D5E88F')
frame_button.place(x=5, y=370, width=335, height=30)
# Listbox
lb = Listbox(master=frame_listbox, selectmode='browse')
for pt in pt_list:
lb.insert('end', pt)
lb.place(x=5, y=5, width=325, height=350)
# Label Text
labelText = Label(master=frame_button, bg='white')
labelText.place(x=5, y=5, width=325, height=20)
# button_choose = Button(master=frame_button, text='Choose', command= lambda: button_click(lb))
action_with_arg = partial(button_click, lb)
button_choose = Button(master=frame_button, text='Choose', command=action_with_arg)
button_choose .place(x=5, y=5, width=325, height=20)
# Aktivierung des Fensters
tk_window.wait_window()
def define_pt_list():
return [
'apple'
]
So far i get the value i want in button_click
but i fail to return it to module A.py
and assign it to filter
. How would i do that? What am i missing here?
I've tried several things including a return button_choose
at the end of get_pt_filter
or lambda functions behind command
while creating Button
. I can not break the Loop however, and i'm stuck forever it seems like. Also i tried tk_window.mainloop()
and tk_window.wait_window()
. None of those work.
In Short: How do i assign "apple" to filter?