#๐Ÿ”’ tkinter tk.Menu behavior ?

4 messages ยท Page 1 of 1 (latest)

lavish talon
#

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?

rough pawnBOT
#

@lavish talon

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

rough pawnBOT
#

@lavish talon

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.