Hello,

I've recently stumbled on a problem while working with the Menu object on both Linux and Windows.

I'm trying to create a loosely-coupled menu object, so rather than calling commands directly I'm generating custom events.  The main application object then binds callback methods to those custom events.

This example shows the basic approach:

**** EXAMPLE FOLLOWS ****

import tkinter as tk


root = tk.Tk()
root.geometry('300x200')
label = tk.Label(text='Test')
label.grid()
menu = tk.Menu(root)

root.configure(menu=menu)

submenu = tk.Menu(menu)
menu.add_cascade(menu=submenu, label='Change Text')
submenu.add_command(
    label='Foo',
    command=lambda: menu.event_generate('<<Foo>>')
)
submenu.add_command(
    label='Bar',
    command=lambda: menu.event_generate('<<Bar>>')
)

def setfoo(*_):
    label.configure(text='Foo')

def setbar(*_):
    label.configure(text='Bar')

menu.bind('<<Foo>>', setfoo)
menu.bind('<<Bar>>', setbar)

print(menu.bind())
root.mainloop()


**** END EXAMPLE ****

This code works as expected on Linux, but does not work at all on Windows.  The callback are never executed.  I suspect this has to do with the way menus are implemented on Windows; I know I can generate/bind events on the root window as a workaround, but should this work as-is?


( PS -- I posted this to stackoverflow, if anyone is interested in replying there:  https://stackoverflow.com/q/67289539/1454109 )


Regards,

Alan Moore

_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss@python.org
https://mail.python.org/mailman/listinfo/tkinter-discuss

Reply via email to