[Tkinter-discuss] Button-press on Menubuttons

Michael Lange klappnase at web.de
Sat Nov 26 19:37:58 CET 2011


Thus spoketh Cam Farnell <msa01 at bitflipper.ca> 
unto us on Sat, 26 Nov 2011 12:54:09 -0400:

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

Not sure, I guess this has something to do with the Menu's event-grabbing.
Maybe a better solution than binding to the Menubuttons is to use the
menu's postcommand option as in:

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')
        self.TA = TA = tk.Menu(self.MenuButtonA)
        TA.add_command(label='Dummy 1')
        TA.add_command(label='Dummy 2')
        self.MenuButtonA['menu'] = TA

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

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

    def HandlerA(self,Event=None):
        self.InfoLabel['text'] = 'Handler A'
        print 'Handler A'
        self.TA.entryconfigure(1, label='FOO')

    def HandlerB(self,Event=None):
        self.InfoLabel['text'] = 'Handler B'
        print 'Handler B'
        self.TB.entryconfigure(1, state='disabled')

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

Best regards


.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

We fight only when there is no other choice.  We prefer the ways of
peaceful contact.
		-- Kirk, "Spectre of the Gun", stardate 4385.3

More information about the Tkinter-discuss mailing list