[Tutor] ttk menubutton
Alan Gauld
alan.gauld at yahoo.co.uk
Wed Apr 12 05:35:25 EDT 2023
On 12/04/2023 06:42, Phil wrote:
> I'm attempting to get the options from a ttk menubutton without a lot of
> success. The menubutton and it's list of options are displayed but I
> don't understand the error message.
>
> # create a menu
> self.menu = tk.Menu(self.mainwindow)
> self.menu.add_command(label="Option 1")
> self.menu.add_command(label="Option 2")
> self.menu.add_command(label="Option 3")
>
> self.menu_button.bind("<ButtonRelease-1>",
> self.on_menu_button_release)
What is menu_button?
It's not mentioned in your code sample until the bind call...
BTW I tried this exercise out of interest and couldn't get
the bind() to work. I'm not sure why, I might do some more
experiments later.
However I must ask why you are using bind in this case - its
more common to just attach the event handler to the menu item
when you create it. Like
self.menu = tk.Menu(self.mainwindow)
self.menu.add_command(label="Option 1",
command=self.on_Option)
Or if you want to use the same handler for multiple items use a lambda
to pass an option:
self.menu = tk.Menu(self.mainwindow)
self.menu.add_command(label="Option 1",
command=lambda : self.on_option(1))
self.menu.add_command(label="Option 2",
command=lambda : self.on_option(2))
def on_option(self, option):
# do something with option here
But binding should work, I'm just not sure how to make it
do so in a menu.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list