[Tutor] ttk menubutton

Phil phillor9 at gmail.com
Wed Apr 12 18:56:31 EDT 2023


On 12/4/23 19:35, Alan Gauld via Tutor wrote:


Thank you Alan for your reply.

> What is menu_button?

Unless I'm mistaken, there are two similar ttk widgets, a menubutton and 
an optionmenu. I think the optionmenu widget might be what I want. I'm 
having no end of trouble maintaining an Internet connection, because I'm 
currently a long way from a telephone tower, so searching for 
information is frustratingly difficult.


> 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)

My AI friend had made many suggestions that didn't work and that was one.

>
> 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

I'll see if I can work your suggestion into what I have, or maybe start 
afresh.

-- 

Regards,
Phil



More information about the Tutor mailing list