import tkinter as tk
root = tk.Tk()
var = tk.StringVar(root)
opmenu = tk.OptionMenu(root, var, *['item %i' % i for i in range(5)])
opmenu.pack()
menu = opmenu['menu']
for i in range(menu.index('end') + 1):
menu.entryconfigure(i, state='disabled')
import tkinter as tk
root = tk.Tk()
root.geometry('250x250+100+100')
str = tk.StringVar()
str.set('Select')
Op = tk.OptionMenu(root, str, "First", "Second", "Third")
Op.pack()
# This will disable the First and Third entries in the Op
# state = 'disable' / 'normal'
Op['menu'].entryconfig(0, state='disable')
Op['menu'].entryconfig("Third", state='disable')
entries = Op['menu'].index('end') # This will get the total no. of entries.
# If you want to disable all of the entries uncomment below 2 lines.
# for i in range(entries+1):
# Op['menu'].entryconfig(i, state='disable')
root.mainloop()
您可以禁用菜单的每个条目,而不是完全使用
menu.entryconfigure(<index>, state='disabled')
禁用optionmenu。 optionmenu的菜单存储在“menu”属性中:因此,您可以查看菜单中的所有项目,但它们不可单击。你知道吗
是的,它可以禁用菜单,但仍然能够打开它只是为了查看列表。在
OptionMenu
中使用的菜单是tkinterMenu()
,您可以访问它。你知道吗示例:
然后您可以使用
Op
菜单执行与Menu()
相同的操作在你的情况下,如何禁用?你知道吗
我们可以根据用户使用
menu.entryconfig(index, options)
来配置state = 'disabled' / 'normal'
。你知道吗示例:
为了更好地理解} class 。(从第3959行开始)
Menu()
是如何在OptionMenu
类中定义的,可以check the source code of ^{相关问题 更多 >
编程相关推荐