Python tkinter Menubutton

t1=tk.Menubutton(parent_window,options)
parent_window : Declared Parent window
options : Various options can be added, list is given below.
Menubutton Tkinter

Options of Tkinter Menubutton linking to radio buttons & entry to configure state & textvariable
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)
l2=tk.Label(my_w,text='Msg here')
l2.grid(row=2,column=2)
str1=tk.StringVar()
font1=('Times',18,'normal')

mb=tk.Menubutton(my_w,text='My Menu',
  activebackground='lightyellow',font=font1,
  underline=4,relief='raised',width=20)
mb.grid(row=2,column=1,padx=10)

mb.menu=tk.Menu(mb)
mb['menu']=mb.menu

int1=tk.IntVar(value=0)
optvar=tk.IntVar(value=1)
def my_fun():
    print("This is inside my_fun")
    l2.config(text='I am from function ')
mb.menu.add_command(label = "Opt 1",command=lambda:print(str1.get() )) 
mb.menu.add_checkbutton(label='Check 1',variable=int1,onvalue=5,offvalue=6,command=lambda:print(int1.get()))
mb.menu.add_radiobutton(label='Radio 1',value=0,variable=optvar,command=lambda:l2.config(text=optvar.get()))
mb.menu.add_radiobutton(label='Radio 2',value=1,variable=optvar,command=lambda:print(optvar.get()))
mb.menu.add_command(label='my_fun',command=lambda:my_fun())
mb.menu.add_command(label='Quit',command=my_w.quit)

my_w.mainloop()  # Keep the window open

Options

activebackgroundColour of the background when Menubutton is active
activeforegroundColour of the foreground ( font ) when Menubutton is active
anchorDirection in which the text to be placed, values can be n, ne, e, se, s, sw, w, nw, or center
bgColor of the background. We can use Hex value or colour name. ( bg='green' or bg='#ffff00')
bdBorder size, default value is 2
bitmap
mb=tk.Menubutton(my_w,bitmap='question',
	relief='raised')
More on Tkinter Bitmap
cursorShape of the cursor while moving over the Entry widget. A list of shapes are available at Button Page.
fgfont colour. Check the example below
fontfont family, font style ,font style to use ( tuple ) . Check the example below
directionOpening of menu. Values can be left, right,below or flush
disabledforegroundColour of the foreground ( font ) when state is disabled.
heightHeight of the menubutton in number of lines.
justifyAlignment of text. Values are 'left','center' or 'right'
padxSpace to be left at left and right side of the text
padySpace to be left at top and bottom side of the text
reliefThe borders 3 D effect style. It can take these values 'raised' , 'sunken' ,'flat', 'ridge', 'solid' & 'groove'
stateValues can be 'disabled', 'normal','active'. Check the example below.
textText to be displayed
textvariablevariable associated with the menubutton text. Usually StringVar is used.
underlineIndex position of text ( starting from 0 ) to be underlined.
widthwidth of the widget, default value is 20
wraplengthLines 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

State option of 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

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
Tkinter menu Tkinter OptionMenu Combobox
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer