简体   繁体   中英

Python Tkinter Menu widget:

I've been trying to learn Tkinter and i've stumbled upon the below code while looking up Menu widget.

from tkinter import *
import tkinter.messagebox


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()

Now i undertand the purpose of code but am having a hard time figuring out what the below line of code is for.

mb["menu"] =  mb.menu

mb["menu"] = Menu() assigns Menu ID to Menubutton - so Menubutton knows what to display.

mb.menu = ... doesn't assign Menu to Menubutton and you can use any other variable ie. mb.hello_world or submenu instead of mb.menu like in example below

import tkinter as tk

root = tk.Tk()

mb = tk.Menubutton(root, text="condiments", relief=tk.RAISED)
mb.grid()

submenu = tk.Menu(mb, tearoff=0)

mayo_var = tk.IntVar()
ketch_var = tk.IntVar()

submenu.add_checkbutton(label="mayo", variable=mayo_var)
submenu.add_checkbutton(label="ketchup", variable=ketch_var)

mb['menu'] = submenu

root.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM