检查tkinter中的菜单项是否已启用或禁用

2024-10-01 00:26:39 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有办法检查tkinter中的菜单项是否已启用

代码如下:

from tkinter import *

root = Tk()
root.geometry("500x500")

def disable_menus():
    test_menu.entryconfig("Sub-Menu 1" , state=DISABLED)
    test_menu.entryconfig("Sub-Menu 2" , state=DISABLED)

def enable_menus():
    test_menu.entryconfig("Sub-Menu 1", state="normal")
    test_menu.entryconfig("Sub-Menu 2", state="normal")

def check_state():
    # Code to check if the sub-menus are enabled or not
    pass

disable_button = Button(root , text = "Disable Menus" , command = disable_menus)
disable_button.grid(row = 0 , column = 0 , padx = 20 , pady = 20)

enable_button = Button(root , text = "Enable Menus" , command = enable_menus)
enable_button.grid(row = 1 , column = 0)

check_button = Button(root , text = "Check State" , command = check_state)
check_button.grid(row = 0 , column = 1)

main_menu = Menu(root)
root.config(menu=main_menu)

test_menu = Menu(main_menu)
main_menu.add_cascade(label = "Test Menu 1" , menu = test_menu)

test_menu.add_command(label = "Sub-Menu 1")
test_menu.add_command(label = "Sub-Menu 2")

mainloop()

在这里,当我单击check_button时,我想检查菜单项是否已启用

在tkinter有没有办法做到这一点

如果有人能帮我就好了


Tags: testmaintkinterenablecheckbuttonrootcommand
1条回答
网友
1楼 · 发布于 2024-10-01 00:26:39

使用menu.entrycget(index, option)这将返回选项的值。 就你而言:

test_menu.entrycget("Sub-Menu 1", 'state')

这将返回“子菜单1”的状态

相关问题 更多 >