I wrote a test bed to learn about button, form and menu bindings. Created a frame and put a button on it and created a menu. Bind'ed (bound?) clicks to the button, menu and form in separate routines.
(Windows 10 and Ubuntu/Pi3) When clicking the form, all is well. When clicking the button, the button routine runs, then the form routine runs, too. Don't really want this. Certainly did not expect it! I put in a global boolean to detect and stop but that seems silly.
How do I restrict the form from also answering a button click without "silly code". I can't imagine why the bind is passed through to the form anyway. What am I missing here?
#!/usr/bin/python3
from tkinter import *
def ButtonClickedOnce(event=None):
global ButtonJustPressed
#Have to nullify the event somehow so the form does not get it, too.
ButtonJustPressed = True # Comment this to see the problem.
print("Button Single Clicked - Primary Button")
pass
def FormClickedOnce(event=None):
global ButtonJustPressed
if ButtonJustPressed: ButtonJustPressed = False
else: print("Form Single Clicked - Primary Button")
pass
def MasterStart(event=None):
print("Let's Go")
pass
def PgmExit(event=None):
print("Primary Double Click on Button or F3, Stopping.")
root.destroy()
pass
ButtonJustPressed = False
root = Tk()
root.wm_title('Bind Test')
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
FM_Start = filemenu.add_command(label="Let's Go", accelerator = "F5",
command = lambda: MasterStart()) # Start 'er up.
FM_Exit = filemenu.add_command(label="Let's Stop", accelerator = "F3",
command = lambda: PgmExit()) # Exit now.
menubar.add_cascade(label='File', menu=filemenu)
root.config(menu=menubar)
frame = Frame(root)
frame.pack()
MyButton = Button(frame, text='Mouse Clicks')
MyButton.pack()
#Mouse button bindings
MyButton.bind('<Button-1>', ButtonClickedOnce)
MyButton.bind('<Double-1>', PgmExit)
root.bind('<Button-1>', FormClickedOnce)
#Key bindings
root.bind('<F3>',PgmExit)
root.bind('<F5>',MasterStart)
root.geometry('%dx%d+%d+%d' % (200, 200, 0, 0))
root.mainloop()