Tkinter Menu

menu in Tkinter GUI

Tkinter menu with options & methods to add checkbox radio buttons items with tearoff and separators
import tkinter  as tk 
my_w = tk.Tk()
my_w.geometry("350x120")  

def my_fun():
    pass
menubar = tk.Menu(my_w)

menu_file = tk.Menu(menubar, tearoff=0,bg='yellow') # file
menu_edit=tk.Menu(menubar,tearoff=0)  # edit menu 

menubar.add_cascade(label="File", menu=menu_file) # Top Line
menubar.add_cascade(label="Edit", menu=menu_edit) # Top Line

menu_file.add_command(label="New", command=my_fun()) # Item 1 of file
menu_file.add_command(label="Open..", command=my_fun()) # Item 2  
#menu_file.add_command(label="Exit", command=lambda:exit()) # Item 3 
#menu_file.add_command(label="Exit", command=my_w.destroy) # Item 3  
menu_file.add_command(label="Exit", command=my_w.quit) # Item 3  

menu_edit.add_command(label="Undo", command=my_fun())# Item 1 of Edit 
menu_edit.add_command(label="Redo", command=my_fun())# Item 2 of Edit 

my_w.config(menu=menubar) # adding menu to window
my_w.mainloop()
We have added option bg='yellow' to set background colour to Yellow. Here is the list of options we can add.

Options :

activebackground

Background colour when mouse is over one option ( choice ) of the menu.

activeborderwidth

Width of the border when mouse is over the menu. default is 1

activeforeground

Foreground colour when mouse is over the menu. ( Font is yellow in bellow picture )

bg

Background colour of the menu. ( The File menu background is Yellow here )
menu_file = tk.Menu(menubar, tearoff=1,bg='yellow',
 activebackground='red',activeborderwidth=3,activeforeground='yellow')
Menu Options active

bd

Border width of the options. (To check , detach the menu by clicking on tearoff=1 and then watch the border width.)

cursor

The shape of cursor when mouse is over the menu. List of cursor shapes are avaialble at Button Page. (To check , detach the menu by clicking on tearoff=1 and then watch the change in Cursor.)

disabledforeground

The colour of the disabled option when cursor is over it.
We have disabled the open.. option
menu_file.entryconfig(2,state='disabled')
menu_file = tk.Menu(menubar, tearoff=1,bg='yellow',
 activebackground='green',activeforeground='yellow',
 disabledforeground='red')
disabledforeground Option

font

my_font1=('Times',16,'bold')
menu_file = tk.Menu(menubar, tearoff=False,bg='yellow',font=my_font1)

fg

menu_file = tk.Menu(menubar, fg='red',tearoff=0,
      bg='yellow',font=my_font1)
fg option of  menu

postcommand

We used postcommand here to run the function Change_color(color) which run once in every 1 second due to recursive call using after(). The fg=color will toggle the font color to red and green every 1 second.
def Change_colour(color):
    if color=='green':
        color='red'
    else:
        color='green'
    menu_file.config(fg=color)    
    my_w.after(1000,Change_colour, color)    
menubar = tk.Menu(my_w)
my_font1=('Times',18,'bold')
menu_file = tk.Menu(menubar, fg='red',tearoff=0,bg='yellow',
font=my_font1, postcommand=lambda: Change_colour('green'))

relief

3-D effect of border of the menu

image

Displaying image on menu.

selectcolor

menu_file = tk.Menu(menubar,title='my title' ,
    tearoff=False,fg='red',bg='yellow',selectcolor='blue')
selectcolor separator option of  menu
Colour of selected Checkbutton and radiobutton.

tearoff

If tearoff is set as 1 ( tearoff=1 ) then the menu can be taken out from the menu bar. Click the dashed line at the top to detach the menu. It can take values True or False.
menu_file = tk.Menu(menubar,title='my title' ,
     tearoff=1,fg='red',bg='yellow')
fg option of  menu

title

Title of the tearoff menu.
menu_file = tk.Menu(menubar, title='my title',tearoff=1,bg='yellow')

Methods

add_command()

Add item to the menu.
menu_file.add_command(label="New", command=my_fun())

add_radiobutton()

Adding radiobutton to the menu.
menu_file.add_radiobutton(label='Option 1',value=0,variable=optvar)

add_checkbutton

Adding checkbutton to the menu
menu_file.add_checkbutton(label='Chk 1',onvalue=1,offvalue=0,variable=chkvar0)
menu_file.add_checkbutton(label='Chk 2',onvalue=1,offvalue=0,variable=chkvar1)
Checkbutton to manage text Wrap

add_cascade

Add menu to the top
menubar.add_cascade(label="File", menu=menu_file)

add_separator()

Check above image for the added separator
menu_file.add_separator()

add

Add specific type of menu items to the menu.

delete

delete from start index to end index
menu_file.delete(0,2)

entryconfig

Disable the item with index position 2 ( check disabledforeground option above )
menu_file.entryconfig(2,state='disabled')

index(item)

Return the index number of input item.
print(menu_file.index('Chk 1')) # 8 

insert_separator(index)

Insert separator at input index position.
menu_file.insert_separator(4)

invoke(index)

For checkbox toggles the selection. For radio button choice is set.
menu_file.invoke(5)

type(index)

Returns the type of choice at the input index position.
print(menu_file.type(5)) # radiobutton
print(menu_file.type(7)) # separator
print(menu_file.type(9)) # checkbutton

Adding Sub menu

Sub-menu with main menu
import tkinter  as tk 
my_w = tk.Tk()
my_w.geometry("250x120")  

def my_fun():
    pass

menubar = tk.Menu(my_w)
my_font1=('Times',12,'bold')
menu_file = tk.Menu(menubar,title='my title' ,
    tearoff=1,fg='red',bg='yellow') # file
menu_edit=tk.Menu(menubar,tearoff=0)  # edit menu 
menu_sub=tk.Menu(menu_file,tearoff=0,bg='green')
menu_file.add_cascade(label='Sub 1',menu=menu_sub ) # add sub
menu_sub.add_command(label='Sub 11',command=my_fun()) 
menu_sub.add_command(label='Sub 12',command=my_fun())

menubar.add_cascade(label="File", menu=menu_file) # Top Line
menubar.add_cascade(label="Edit", menu=menu_edit) # Top Line

menu_file.add_command(label="New", command=my_fun()) # Item 1 of file
menu_file.add_command(label="Open..", command=my_fun()) # Item 2  
menu_file.add_separator()

menu_file.add_command(label="Exit", command=my_w.quit) # Item 3  
menu_edit.add_command(label="Undo", command=my_fun())# Item 1 of Edit 
menu_edit.add_command(label="Redo", command=my_fun())# Item 2 of Edit 

my_w.config(menu=menubar) # adding menu to window
my_w.mainloop()

Adding sub menu to sub menu

Sub menu of Sub menu
menu_sub2=tk.Menu(menu_sub,tearoff=0,bg='red')
menu_sub.add_cascade(label='Sub 2',menu=menu_sub2)
menu_sub2.add_command(label='Sub 21',command=my_fun())
menu_sub2.add_command(label='Child ',command=lambda:my_child())

Open new window on Menu click

Child window  using Toplevel
We can open a child window on click of a menu item. To work on Toplevel we have to add this line at the top.
from tkinter import *
from tkinter import *
def my_child():
    my_w_child=Toplevel(my_w) # Child window 
    my_w_child.geometry("200x200")  # Size of the window 
    my_w_child.title("www.plus2net.com")
    my_str1 = tk.StringVar()
    l1 = tk.Label(my_w_child,  textvariable=my_str1 )
    l1.grid(row=1,column=2) 
    my_str1.set("Hi I am Child window")
menubar = tk.Menu(my_w)
menu_file = tk.Menu(menubar,tearoff=0) # file
menu_edit=tk.Menu(menubar,tearoff=0)  # edit menu 
menubar.add_cascade(label="File", menu=menu_file) # Top Line
menubar.add_cascade(label="Edit", menu=menu_edit) # Top Line
menu_file.add_command(label="New window", command=lambda:my_child())
menu_file.add_command(label="Exit", command=my_w.quit)
Tkinter window Adding Sub menu to main menu and opening child window from menu command

OptionMenu Menubutton Combobox Projects in Tkinter
Managing Font option using Menu
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    11-04-2023

    always helpful

    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