1

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
7
  • 1
    you don't have to put spaces around ( and ) - see PEP 8 -- Style Guide for Python Code Commented Jan 9, 2018 at 10:49
  • remove line mb["menu"] ans see what's happen. Commented Jan 9, 2018 at 10:51
  • @PEP 8 -- Style Guide for Python Code – furas the code functions i just have an issue with understanding the line "mb["menu"] = mb.menu" Commented Jan 9, 2018 at 10:54
  • line 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. submenu instead of mb.menu Commented Jan 9, 2018 at 10:54
  • It is easier to answer for problem if code is correctly formated. Commented Jan 9, 2018 at 10:54

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

1 Comment

Further note that, mb['menu'] = submenu is practically singular option configuration equivalent of mb.config(menu=submenu) and mb.configure(menu=submenu).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.