tkinter menu checkbutton - 阻止菜单关闭

时间:2016-04-28 10:24:28

标签: python tkinter

有没有办法在点击检查按钮时保持tkinter菜单打开? 在示例程序中单击菜单项时菜单列表消失,因此您需要再次打开菜单以单击下一项。 那么有没有办法保持菜单的选择,以便点击多个复选框?

from Tkinter import *

def click():
    pass

root = Tk()
menu = Menu(root)
root.config(menu=menu)
choicesmenu = Menu(menu,tearoff=0)

menu.add_cascade(label="Choices", menu=choicesmenu)
choicesmenu.add_checkbutton(label="choice1", command=click)
choicesmenu.add_checkbutton(label="choice2", command=click)
choicesmenu.add_checkbutton(label="choice3", command=click)
choicesmenu.add_checkbutton(label="choice4", command=click)
choicesmenu.add_checkbutton(label="choice5", command=click)
choicesmenu.add_checkbutton(label="choice6", command=click)
choicesmenu.add_checkbutton(label="choice7", command=click)

mainloop()

1 个答案:

答案 0 :(得分:1)

我建议你不要做你想要的,因为你正在寻找扰乱菜单意图实现的正常行为。看一下浏览器的菜单行为,你会发现它的表现并不像你想要的那样。幸运的是这种情况,否则如果您的行为已经实施,我们将需要执行额外的点击,以便我们从菜单中选择某些内容以便稍后隐藏它。

除此之外,你完全打破我建议你阅读的Principles of User Interface Design

你会告诉我你的应用程序无论如何都需要这种行为;我的回答是什么,再次反对:如果你的应用程序需要这种行为,那么你的设计是错误的,我鼓励在尝试编程之前重新考虑你的设计。

但是,仅仅为了好奇,您可以查看:Keep a menu open in Tkinter