Menubutton Basic

Messagebox, Menubutton, Menu, Checkbutton, add

Note that Menubutton is obsolete (a nice way of saying it is probably going to be deprecated soon) according to Python.org,  Having said that…..

You don’t need to use a whole messagebar if you have just a simple menu but it seems to me the menubutton makes things a lot more complicated and actually does not free up much if any screen geography.  Maybe there is a point to it that I am missing – would really love to hear from somebody who has an opinion.  Be that as it may, here is an example of how it works using a general idea borrowed from Shipman.

Actually, this demo is as much about Checkbutton and Messagebox – Messagebox is not a widget but a separate module –   as it is about Menubutton.


#Testing menubutton, menu, add, chekbutton, messagebox
#standard set up header code 2 
from tkinter import *
root = Tk()
root.attributes('-fullscreen', True)
root.configure(background='SteelBlue4')
scrW = root.winfo_screenwidth()
scrH = root.winfo_screenheight()  
workwindow = str(1024) + "x" + str(768)+ "+" +str(int((scrW-1024)/2)) + "+" +str(int((scrH-768)/2))
top1 = Toplevel(root, bg="light blue")
top1.geometry(workwindow)
top1.title("Top 1 - Workwindow")
top1.attributes("-topmost", 1)  # make sure top1 is on top to start
root.update()                   # but don't leave it locked in place
top1.attributes("-topmost", 0)  # in case you use lower or lift
#exit button - note: uses grid
b3=Button(root, text="Egress", command=root.destroy)
b3.grid(row=0,column=0,ipadx=10, ipady=10, pady=5, padx=5, sticky = W+N)
#____________________________
from tkinter import messagebox

def mboxcondiments():
    msgStr='mayo: '+str(mayoVar.get())+" ketchup: "+str(ketchVar.get())
    messagebox.showinfo("About Condiments", msgStr, parent=top1)
    
myMb= Menubutton (top1, text="Condiments", indicatoron=True, justify="left") #first have to create a menubutton
myMb.pack(anchor=W)  #pack(anchor=W) works

myMb.menu= Menu(myMb, tearoff=0 ) # create a menu as a child of your Menubutton
print("type: "+ str(type(myMb["menu"]))) # only doing that is not enough
print("->: "+ str(myMb["menu"]))  # see on your console the string is bland
myMb["menu"] =  myMb.menu  # set the menu attribute of your Menubutton
print("type: "+ str(type(myMb["menu"]))) #it still comes off as a string
print("->: "+ str(myMb["menu"])) # but the string is now a pathname
mayoVar= IntVar()
mayoVar.set(0)  #0 or 1 - defines state
ketchVar= IntVar()
mayoVar.set(0)
               # add can be cascade, checkbutton, radiobutton, separator, or command
myMb.menu.add_checkbutton (label="mayo", variable= mayoVar, command= mboxcondiments)
myMb.menu.add_checkbutton (label="ketchup", variable= ketchVar, command=mboxcondiments)

myMb.pack()

#____________________________
root.mainloop()