Header Ads

Header ADS

Create Menubar in tkinter


Tkinter : Menu widget

The goal of this widget is to allow us to create all kinds of menus that can be used by our applications. The core functionality provides ways to create three menu types: pop-up, toplevel and pull-down.

In this article we are creating  3 types of menubar .

So lets Code :

from tkinter import *

def donothing():
   filewin = Toplevel(root)
   button = Button(filewin, text="Do nothing button")
   button.pack()
   
root = Tk()
root.title("Dynamic Coding")
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)

newmenu = Menu(filemenu)
newmenu.add_command(label="New text file", command=donothing)
newmenu.add_command(label="New py file", command=donothing)
newmenu.add_command(label="New docx file", command=donothing)

filemenu.add_cascade(label='New',menu=newmenu)

#check = Checkbutton()
	
#filemenu.add_checkbutton(filemenu,text='Dark Mode')
filemenu.add_command(label="Open", command=donothing)
filemenu.add_command(label="Save", command=donothing)
filemenu.add_command(label="Save as...", command=donothing)
filemenu.add_command(label="Close", command=donothing)

filemenu.add_separator()

filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)

# sub menu to file :


editmenu = Menu(menubar, tearoff=0)
editmenu.add_command(label="Undo", command=donothing)

editmenu.add_separator()

editmenu.add_command(label="Cut", command=donothing)
editmenu.add_command(label="Copy", command=donothing)
editmenu.add_command(label="Paste", command=donothing)
editmenu.add_command(label="Delete", command=donothing)
editmenu.add_command(label="Select All", command=donothing)

menubar.add_cascade(label="Edit", menu=editmenu)

root.config(menu=menubar)
root.mainloop()

The above code will create a menubar and sub menubar :


Output:





Menubar with checkbutton :

from tkinter import *

root = Tk()
root.title("Dynamic Coding")

menubar = Menu(root)
show_all = BooleanVar()
show_all.set(True)
show_done = BooleanVar()
show_not_done = BooleanVar()

view_menu = Menu(menubar)
view_menu.add_checkbutton(label="Show All", onvalue=1 offvalue=0, 
variable=show_all)
view_menu.add_checkbutton(label="Show Done", onvalue=1, offvalue=0,
 variable=show_done)
view_menu.add_checkbutton(label="Show Not Done", onvalue=1,offvalue=0, 
variable=show_not_done)

menubar.add_cascade(label='View', menu=view_menu)

root.config(menu=menubar)

root.mainloop()

as you can we use a add_checkbutton method to add checkbutton .

Output:
you can remove the dashed lines ( ---- ) by using tearoff =0 as like used in first code .


Menubar with Radiobutton :

# Tkinter Tutorial : Dynamic Coding
# Menu Widget ......
from tkinter import *

def func():
	top = Toplevel()
	top.mainloop()


def theme():
	if radio.get() == 1 :
		root.configure(bg = 'red')
	elif radio.get() == 2:
		root.configure(bg = 'pink')
	elif radio.get() == 3:
		root.configure(bg = 'cyan')
	elif radio.get() == 4:
		root.configure(bg = 'purple')
	else :
		root.configure(bg='white')



root = Tk()
root.title("Tkinter tutorial")
root.geometry('300x200+300+200')

radio = IntVar()
#radio.set(1)

menubar = Menu(root)
# creating file menu ..
file_menu = Menu(menubar,tearoff=0)
# adding content on file menu ..

file_menu.add_radiobutton(label='Red',
	value = 1,
	variable= radio,command=theme)

file_menu.add_radiobutton(label='Pink',
	value = 2,
	variable= radio,command=theme)

file_menu.add_radiobutton(label='cyan'
	,value = 3,
	variable= radio,command=theme)

file_menu.add_radiobutton(label='purple',
	value = 4,
	variable= radio,command=theme)



file_menu.add_separator()
file_menu.add_command(label='Open',
	command=func)


file_menu.add_separator()
menubar.add_cascade(label='File',
	menu= file_menu)

root.config(menu=menubar)

theme()
root.mainloop()

add_radiobutton method is used to add redio button in menubar but the differencr the radiobutton is looks like checkbutton but you can only select one value at a time as according radiobutton property.

Output:




Attributes of Menu Widget :

activebackground :
The background color that will appear on a choice when it is under the mouse.

activeborderwidth :
Specifies the width of a border drawn around a choice when it is under the mouse. Default is 1 pixel.

activeforeground :
The foreground color that will appear on a choice when it is under the mouse.

bg :
The background color for choices not under the mouse.

bd :
The width of the border around all the choices. Default is 1.

cursor :
The cursor that appears when the mouse is over the choices, but only when the menu has been torn off.

disabledforeground :
The color of the text for items whose state is DISABLED.

font :
The default font for textual choices.
fg :
The foreground color used for choices not under the mouse.

postcommand :
You can set this option to a procedure, and that procedure will be called every time someone brings up this menu.
relief :
The default 3-D effect for menus is relief=RAISED.

image :
To display an image on this menubutton.

selectcolor :
Specifies the color displayed in checkbuttons and radiobuttons when they are selected.

tearoff :
Normally, a menu can be torn off, the first position (position 0) in the list of choices is occupied by the tear-off element, and the additional choices are added starting at position 1. If you set tearoff=0, the menu will not have a tear-off feature, and choices will be added starting at position 0.
title :
Normally, the title of a tear-off menu window will be the same as the text of the menubutton or cascade that lead to this menu. If you want to change the title of that window, set the title option to that string.

Methods of Menu Widget :

add_command (options) :
Adds a menu item to the menu.


add_radiobutton( options ) :
Creates a radio button menu item.

add_checkbutton( options ) :
Creates a check button menu item.

add_cascade(options) :
Creates a new hierarchical menu by associating a given menu to a parent menu


add_separator() :
Adds a separator line to the menu.


add( type, options ) :
Adds a specific type of menu item to the menu.


delete( startindex [, endindex ]) :
Deletes the menu items ranging from startindex to endindex.

entryconfig( index, options ) :
Allows you to modify a menu item, which is identified by the index, and change its options.

index(item) :
Returns the index number of the given menu item label.

insert_separator ( index ) 
Insert a new separator at the position specified by index.


invoke ( index ) :
Calls the command callback associated with the choice at position index. If a checkbutton, its state is toggled between set and cleared; if a radiobutton, that choice is set.
type ( index )

Returns the type of the choice specified by index: either "cascade", "checkbutton", "command", "radiobutton", "separator", or "tearoff".






This Article is Contributed by Yogesh singh .
Share your suggestions and feedback in comment sections.

Thank you for visiting here ..

No comments

Powered by Blogger.