I figured out how to make it work, but I don't understand why it works this way and not in a simpler way. Can anyone here explain this?
import tkinter as tk
class MenuPopup(tk.Menu):
def __init__(self, parent):
super().__init__(parent, tearoff=0)
self.parent = parent
self.add_command(label='Replace', command=lambda e=0: self.user_decision(e))
self.add_command(label='Add', command=lambda e=1: self.user_decision(e))
def user_decision(self, user_dec):
self.parent.user_selection.set(user_dec)
print(self.parent.user_selection.get(), f' - Step 3 - changed to user dec: ({user_dec})')
def custom_popup(self, x, y):
self.parent.user_selection.set(-1)
print(self.parent.user_selection.get(), ' - Step 2 - changed to (-1)')
self.tk_popup(x, y)
print(self.parent.user_selection.get(), ' - Step 4 - supposed to be changed...')
class TestApp(tk.Tk):
def __init__(self):
super().__init__()
self.user_selection = tk.IntVar()
self.bind_all('<Button-3>', self._invoke)
self.m = MenuPopup(self)
def _invoke(self, event):
x, y = self.winfo_pointerxy()
print('')
print(self.user_selection.get(), ' - Step 1 - before change')
self.m.custom_popup(x, y)
print(self.user_selection.get(), ' - Step 5 - but is not...')
self.after(0, lambda: print(self.user_selection.get(), ' - Step 6 - now is ok ?!'))
if __name__ == '__main__':
app = TestApp()
app.mainloop()
I store the user_selection variable in master. In separate MenuPopup class you can change this parameter.
Question: why is it not changed immediately, but after some time/cycle?