Skip to main content
 首页 » 编程设计

python之tkinter menu checkbutton之防止菜单关闭

2025年04月02日17luoye11

有没有办法在点击检查按钮时保持 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() 

请您参考如下方法:

我建议您不要做您正在寻找的事情,因为您希望破坏菜单旨在实现的正常行为。查看浏览器的菜单行为,您会发现它的行为方式与您想要的不同。幸运的是这种情况,否则如果您的行为被实现,我们将需要在我们从菜单中选择某些东西的任何时候执行额外的点击,以便稍后隐藏这个。

除此之外,您完全破坏了 Principles of User Interface Design我建议您阅读。

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

不过,出于好奇,您可以查看:Keep a menu open in Tkinter