font family, font style ,font style to use ( tuple ) . Check the example below
direction
Opening of menu. Values can be left, right,below or flush
disabledforeground
Colour of the foreground ( font ) when state is disabled.
height
Height of the menubutton in number of lines.
justify
Alignment of text. Values are 'left','center' or 'right'
padx
Space to be left at left and right side of the text
pady
Space to be left at top and bottom side of the text
relief
The borders 3 D effect style.
It can take these values 'raised' , 'sunken' ,'flat', 'ridge', 'solid' & 'groove'
state
Values can be 'disabled', 'normal','active'. Check the example below.
text
Text to be displayed
textvariable
variable associated with the menubutton text. Usually StringVar is used.
underline
Index position of text ( starting from 0 ) to be underlined.
width
width of the widget, default value is 20
wraplength
Lines to be broken ( next line ) after the input number of chars.
All options of menu like postcommand, add_command(), add_radiobutton(),add_checkbutton(),add_cascade etc can be added. Read the tkinter menu tutorial for more details.
State Option to disable menubutton
We will use radio buttons to change the state option of the Menubutton. On click of radio buttons the Config method of Menubutton is used to change the state to three different values, disabled, active and normal.
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("410x250") # Size of the window
l1=tk.Label(my_w,text='Welcome to Menubutton')
l1.grid(row=1,column=1)
r_v=tk.IntVar()
str1=tk.StringVar()
font1=('Times',18,'normal')
mb=tk.Menubutton(my_w,text='My Menu',
activebackground='yellow',font=font1,
underline=4,relief='raised',width=15)
mb.grid(row=2,column=1,padx=3)
mb.menu=tk.Menu(mb)
mb['menu']=mb.menu
mb.menu.add_command(label = "Opt 1")
mb.menu.add_command(label='Quit',command=my_w.quit)
r1 = tk.Radiobutton(my_w, text='Disable', variable=r_v,
value='1',command=lambda:mb.config(state='disabled'))
r1.grid(row=2,column=2)
r2 = tk.Radiobutton(my_w, text='Active', variable=r_v,
value='2',command=lambda:mb.config(state='active'))
r2.grid(row=2,column=3)
r3 = tk.Radiobutton(my_w, text='Normal', variable=r_v,
value='3',command=lambda:mb.config(state='normal'))
r3.grid(row=2,column=4)
my_w.mainloop() # Keep the window open
Managing textvariable option of Menubutton
We will use one String variable ( str1 ) and through this we will change the text appearing on the Menubutton. This string variable ( str1 ) is also connected to Entry button where user can input data to update the text appearing on the Menubutton.
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("410x250") # Size of the window
l1=tk.Label(my_w,text='Welcome to Menubutton')
l1.grid(row=1,column=1,pady=20,padx=10)
r_v=tk.IntVar()
str1=tk.StringVar(value='My Menu')
font1=('Times',18,'normal')
mb=tk.Menubutton(my_w,activebackground='yellow',font=font1,
underline=4,relief='raised',width=15,textvariable=str1)
mb.grid(row=2,column=1,padx=3)
mb.menu=tk.Menu(mb)
mb['menu']=mb.menu
mb.menu.add_command(label = "Opt 1")
mb.menu.add_command(label='Quit',command=my_w.quit)
e1=tk.Entry(my_w,textvariable=str1)
e1.grid(row=2,column=2)
my_w.mainloop() # Keep the window open