
The Pyhton Tkinter Menu Bar
Post by Amina Delali, January 09th, 2023
Description
The python TK Menu widget, is the widget that allows you to create bar menus as well as submenus for your application.
All you have to do is to (after creating the main window of the application):
- Create the bar menu calling the constructor function Menu.
- Create the associated submenu by calling the same constructor function: Menu.
-
Create the menu items of the submenu by calling the add_command method from the created submenu. The order of the calling, will correspond to the order of the created items.
You can also call other methods, to add different types of items and elements, like:
- add_radiobutton: to add a radio button item.
- add_checkbutton: to add a check button item.
- add_separator: to add a seperator element.
- add_cascade: to add a submenu element. This is the function that we are going to use to add our submenu to the bar menu.
- Add the submenu to the bar menu by calling the add_cascade method.
- Now, you have to associate the created bar menu to the main window of the application, by setting the menu attribute of the main window to that bar menu.
In the code below, we are going to create a bar menu with one submenu. But, we are going to add other buttons in other to change some of the attributes of the created elements:
- The tearoff value : When you create the submenu, you can either set the tearoff value to 0, so there will be no clear separation between the bar menu or the added submenu. Or set the value to 1. In that case, a separative dashed line will be added before the submenu. The ButtonTO button will call the function changeTO that will change the value between 1 and 0.
- The state value: an menu item can have its state either ACTIVE, NORMAL, or DISABLED. To disable the item, we will set it to DISABLED. To enable it, we will set it to NORMAL. To create this behavior, we use two buttons: ButtonES and ButtonDS, that call respectively the functions enableS and disableS. To change the state value of the menu item, we will call the entryconfig method from the created submenu.
The Code
from tkinter import *
from tkinter import messagebox
main_window=Tk()
main_window.title("Python Tkinter Menu Bar")
main_window.geometry('500x450+400+100')
main_window.configure(bg="#8800FF")
def showMessage(label):
messagebox.showinfo("Clicked on a menu button","I clicked on "+label)
menuBar = Menu(main_window)
fileMenu = Menu(menuBar,tearoff=0)
fileMenu.add_command(label="New",
command= lambda: showMessage("New") )
fileMenu.add_command(label="Open",
command= lambda: showMessage("Open"))
fileMenu.add_command(label="Save", state=DISABLED,
command= lambda: showMessage("Save"))
fileMenu.add_command(label="Save As ...", state=DISABLED,
command= lambda: showMessage("Save As"))
fileMenu.add_separator()
fileMenu.add_command(label="Quit",
command=main_window.quit)
menuBar.add_cascade(label="File", menu=fileMenu)
labelTO = Label(main_window,text="tearoff = 0",
bg="#8800FF", fg="white",
font=("Times",16,"bold"))
labelTO.place(rely=0.40,relx=0.5, anchor=CENTER)
def changeTO():
fileMenu["tearoff"]=not fileMenu["tearoff"]
labelTO["text"] = "tearoff = "+str(int(fileMenu["tearoff"] ))
def enableS():
fileMenu.entryconfig("Save",state=NORMAL)
def disableS():
fileMenu.entryconfig("Save",state=DISABLED)
ButtonTO = Button(main_window,text="Change Tearoff value",
bg="white",fg="#8800FF",
command=changeTO)
ButtonTO.place(rely=0.5,relx=0.5,anchor=CENTER)
ButtonES = Button(main_window,text="Enable Save",
bg="#00fa92",fg="#8800FF",
command=enableS)
ButtonES.place(rely=0.65,relx=0.5,anchor=CENTER)
ButtonDS = Button(main_window,text="Disable Save",
bg="#ff0073",fg="white",
command=disableS)
ButtonDS.place(rely=0.75,relx=0.5,anchor=CENTER)
main_window.config(menu=menuBar)
main_window.mainloop()
The Output
Something to say?
If you want to add something about this post, please feel free to do it by commenting below 🙂.