【发布时间】:2016-11-25 06:48:21
【问题描述】:
我编写了一个测试平台来了解按钮、表单和菜单绑定。创建一个框架并在其上放置一个按钮并创建一个菜单。绑定(绑定?)在单独的例程中单击按钮、菜单和表单。
(Windows 10 和 Ubuntu/Pi3)单击表单时,一切正常。单击按钮时,按钮例程运行,然后表单例程也运行。真的不想要这个。肯定没想到吧!我输入了一个全局布尔值来检测和停止,但这似乎很愚蠢。
如何限制表单在没有“愚蠢代码”的情况下也回答按钮单击。我无法想象为什么绑定无论如何都会传递给表单。我在这里错过了什么?
#!/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()
【问题讨论】:
标签: python python-3.x button tkinter bind