我有一个下拉菜单,其中的选项为state="disabled"
,除非列表中包含项目。但是,菜单仅在创建时检查列表,而不是每次用户打开菜单时都检查。如何动态更新状态?
from tkinter import *
window = Tk()
window.title("New Test")
window.geometry("300x300")
my_list = []
root_menu = Menu(window)
window.config(menu=root_menu)
def addList():
global my_list
my_list.append("one")
print(my_list)
def isListEmpty(theList):
if len(theList) > 0:
return "active"
else:
return "disabled"
def showList():
global my_list
print(my_list)
first_menu = Menu(root_menu, tearoff=False)
root_menu.add_cascade(label="File", menu=first_menu)
first_menu.add_command(label="Add to List", command=addList)
first_menu.add_command(label="Show List", state=isListEmpty(my_list), command=showList)
window.mainloop()