选中项目时如何阻止包含复选按钮的 tkinter OptionMenu 关闭

How to stop tkinter OptionMenu that contains checkbutton from closing when an item is checked

我有一个 OptionMenu 小部件,我添加了一些复选按钮。当一个项目被检查时它会一直关闭,我希望它停止这样做这是代码

drop1=OptionMenu(frame2, variable=clicked1,value="Options :")
other_variables={}
for o in other:
drop1['menu'].addcheckbutton(label=o,onvalue=1,offvalue=2,variable=var4, command=checkedOther)

Other is a list containing the items that need to be selected

您无法阻止菜单关闭,但您可以在 checkedOther() 函数中将其显示回来:

def checkedOther(*args):
    # show the popup menu
    x, y, h = drop1.winfo_rootx(), drop1.winfo_rooty(), drop1.winfo_height()
    drop1['menu'].post(x, y+h)