In a Python script, I display twice the same tkinter dialog.
The first time, it's correctly filled with the value returned from get_items()
, the second time the list is shown empty.
Here is a simplified version of my code, that reproduces the behavior:
import tkinter as tk
class ItemSelector:
def __init__(self, title='title'):
self.selection = None
self.root = tk.Tk()
self.root.attributes('-type', 'dialog')
self.root.title(title)
self.root.bind('<Escape>', self.cancel)
self.root.bind('<Return>', self.send)
frame = tk.Frame(self.root) # we need a frame, if we want scrollbar
frame.pack()
items = self.get_items()
self.items_tk = tk.Variable(value=items) # FIX: tk.Variable(frame, value=items)
self.items_ui = tk.Listbox(frame, listvariable=self.items_tk, height=12)
scrollbar = tk.Scrollbar(frame, orient="vertical")
scrollbar.config(command=self.items_ui.yview)
scrollbar.pack(side="right", fill="y")
self.items_ui.config(yscrollcommand=scrollbar.set)
self.items_ui.pack()
submitButton = tk.Button(self.root, text='Submit', command=self.send)
submitButton.pack()
self.root.mainloop()
def get_items(self):
return ['a', 'b', 'c', 'd']
def cancel(self, *args):
self.root.quit()
self.root.withdraw()
def send(self, *args):
self.root.withdraw()
self.root.quit()
def main():
itemSelector = ItemSelector('A')
itemSelector = ItemSelector('B')
if __name__ == '__main__':
main()
How can I get the list of items to be shown also in the second call?
First solution
Swifty's answer helped me understand and circumscribe the issue: It's because I have multiple Tk instances.
His solution with root
as a class variable does work, but, if I have one single root, I'd prefer to have it outside of the list dialog and share it with both classes. (I don't have working code for that solution but I will post if and when I get to that.)
As a first solution, I've added a #FIX
in the original code applying TheLizzard's hints from https://stackoverflow.com/a/69062053/5239250 to my code: if you have multiple Tk instances you need to always explicitly pass the Tk context when creating Tk variables!