[Tkinter-discuss] Button-press on Menubuttons

Cam Farnell msa01 at bitflipper.ca
Sat Nov 26 17:54:09 CET 2011

I have a bunch of Menubuttons in my application, lets call them A, B and C.

Each Menubutton is bound to <ButtonPress-1> so, before the menu choices are displayed, I can enable/disable some of those choices depending on the situation at the time, and each binding leads to its own Menubutton-specific handler.

If the user clicks on Menubutton-A then the corresponding handler runs as expected. If, without making a choice from menu A, the user then clicks on Menubutton-B then - and this is the nub of the problem - the handler bound to Menubutton-B does NOT run. If the user now clicks on Menubutton-C, then the handler for B, yes B, runs.

Is this a bug, a feature or me being stupid? I'm running Python 2.6.5 on Ubuntu 10.04. The simplest demonstration example I could manage follows. Clicking on Menubutton A shows, in the label to the right of the Menubuttons, that the handler ran. If you then click immediately (without selecting any of the dummy menu choices or otherwise making the open menu go away) on Menubutton B then the handler does not run. If you then click on Menubutton A (or for that, click again in Menubutton B) then the handler for B runs.

I've already developed a workaround for my particular application, but this strikes me as anomalous behavior.


Cam Farnell

import Tkinter as tk

class Application(tk.Frame):
     def __init__(self, master=None):
         tk.Frame.__init__(self, master)

         self.MenuButtonA = tk.Menubutton(self,text='Menu Button A')
         T = tk.Menu(self.MenuButtonA)
         T.add_command(label='Dummy 1')
         T.add_command(label='Dummy 2')
         self.MenuButtonA['menu'] = T

         self.MenuButtonB = tk.Menubutton(self,text='Menu Button B')
         T = tk.Menu(self.MenuButtonB)
         T.add_command(label='Dummy 1')
         T.add_command(label='Dummy 2')
         self.MenuButtonB['menu'] = T

         self.InfoLabel = tk.Label(self,text='---------')

     def HandlerA(self,Event):
         self.InfoLabel['text'] = 'Handler A'
     def HandlerB(self,Event):
         self.InfoLabel['text'] = 'Handler B'

app = Application()
app.master.title("Sample application")

More information about the Tkinter-discuss mailing list