我创建了一个Menu
:
self.commandMenu = Menu(menubar, tearoff = 0)
self.commandMenu.add_command(label = "Rename", accelerator = "Ctrl+R", command = self.onRename, state = "disabled")
self.commandMenu.add_command(label = "Edit module name", command = self.onEditModuleName, state = "disabled")
self.commandMenu.add_command(label = "Create instance", command = self.onCreateInstance, state = "disabled")
self.commandMenu.add_command(label = "Delete instance", command = self.onDeleteInstance, state = "disabled")
我计划稍后配置所有项目,但以下代码一次配置1个项目:
self.commandMenu.entryconfig(1, state = "normal")
有没有办法获取菜单项的总数,或者一次配置所有项目?
答案 0 :(得分:4)
您可以使用self.commandMenu.index("end")
获取菜单中最后一项的索引。然后,您可以遍历项目以对它们进行全部配置:
last = self.commandMenu.index("end")
for i in range(last+1):
self.commandMenu.entryconfigure(i, state="normal")
答案 1 :(得分:1)
[更新Bryan Oakley的答案]
<Trans id={"hello"}>
<strong>world!</strong>
</Trans>
msgid "hello"
msgstr "Hello <0> world! </0>"
不起作用。
请改用self.commandMenu.index("end")
。
[编辑]
当菜单中没有元素时,请确保捕获错误:self.commandMenu.index(tk.END)
将返回index(tk.END)
并且您将遇到错误None