单击时如何使tkinter中的菜单按钮执行特定功能

时间:2018-11-18 10:06:29

标签: python-3.x python-2.7 tkinter

我还是复制了这段代码

from Tkinter import * import tkMessageBox import Tkinter

top = Tk()

mb= Menubutton ( top, text="condiments", relief=RAISED ) mb.grid() mb.menu = Menu ( mb, tearoff = 0 ) mb["menu"] = mb.menu

mayoVar = IntVar() ketchVar = IntVar()

mb.menu.add_checkbutton ( label="mayo", variable=mayoVar ) mb.menu.add_checkbutton ( label="ketchup", variable=ketchVar )

mb.pack() top.mainloop()

当某人单击mayo ..我希望它执行特定功能

1 个答案:

答案 0 :(得分:1)

使用回调函数来实现所需的操作,然后从菜单项中调用它:

def callback_function():
    # Your code here

mb.menu.add_checkbutton(label="mayo", variable=mayoVar,
                        command=callback_function)