In this Python tutorial, we will learn about the Python Tkinter menu bar. And also we will cover these topics.
- Python Tkinter Menu bar
- Python Tkinter Menu bar color
- Python Tkinter Menu bar background-color
- Python Tkinter Menu bar Checkbox
- Python Tkinter Menu bar Class
- Python Tkinter Menu bar icon
- Python Tkinter Menu bar submenu
If you are new to Python GUI programming, then check out Python GUI Programming (Python Tkinter).
Python Tkinter Menu bar
- Tkinter Menu bar is used to create menu options that can provide additional functionality to the application
- The menu bar is very common among software & applications.
- Here is an example of a menu bar
- In this picture, File, Edit, Section, etc are menu bars, and the menu options as New File, New window, etc.
- Few of them also have a sub-menu like in the case of open Recent. The small arrow indicates the possible expansion.
- You must have seen this kind of menu bar in many software & application.
- Now when you have understood how the menu bar looks like, let’s understand how to create them in Tkinter.
You may also like, How to display data in textboxes using Python Tkinter? and How to Set Background to be an Image in Python Tkinter?
Python Tkinter Menu bar color
Let us see how to give color in Python Tkinter menu bar.
- Coloring a menubar is possible only in the Linux machine.
- The reason for that is Tkinter outsources the menu bar that is why have limited options.
- To implement coloring on Linux machines use keyword background to set the background color and foreground to set the text color.
Code:
Here is the implementation of the menubar on Linux. In this, we have created 3 popular menu bars File, Edit, and Help. Each menu bar has options. Out of these Exit & About are functional.
from tkinter import *
from tkinter import messagebox
ws =Tk()
ws.title("Python Guides")
ws.geometry("300x250")
def about():
messagebox.showinfo('PythonGuides', 'Python Guides aims at providing best practical tutorials')
menubar = Menu(ws, background='#ff8000', foreground='black', activebackground='white', activeforeground='black')
file = Menu(menubar, tearoff=1, background='#ffcc99', foreground='black')
file.add_command(label="New")
file.add_command(label="Open")
file.add_command(label="Save")
file.add_command(label="Save as")
file.add_separator()
file.add_command(label="Exit", command=ws.quit)
menubar.add_cascade(label="File", menu=file)
edit = Menu(menubar, tearoff=0)
edit.add_command(label="Undo")
edit.add_separator()
edit.add_command(label="Cut")
edit.add_command(label="Copy")
edit.add_command(label="Paste")
menubar.add_cascade(label="Edit", menu=edit)
help = Menu(menubar, tearoff=0)
help.add_command(label="About", command=about)
menubar.add_cascade(label="Help", menu=help)
ws.config(menu=menubar)
ws.mainloop()
Output:
In this output, colourful menu bar is demonstrated. The background is “Orange” and foreground is “Black”.
Read: Python Tkinter Colors
Python Tkinter Menu bar Checkbox
- Checkbutton or Checkbox allows switching between options
- They are straight forward & returns the value as true or false
- Menu bar with check box can be used for scenarios like choosing the dark or light mode, Hidden items, filtering, etc.
Code:
This code shows the implementation of menu bar checkbox.
from tkinter import *
from tkinter import messagebox
ws =Tk()
ws.title("Python Guides")
ws.geometry("300x250")
def about():
messagebox.showinfo('PythonGuides', 'Python Guides aims at providing best practical tutorials')
def darkMode():
if darkmode.get() == 1:
ws.config(background='black')
elif darkmode.get() == 0:
ws.config(background='white')
else:
messagebox.showerror('PythonGuides', 'Something went wrong!')
menubar = Menu(ws, background='#ff8000', foreground='black', activebackground='white', activeforeground='black')
file = Menu(menubar, tearoff=1, background='#ffcc99', foreground='black')
file.add_command(label="New")
file.add_command(label="Open")
file.add_command(label="Save")
file.add_command(label="Save as")
file.add_separator()
file.add_command(label="Exit", command=ws.quit)
menubar.add_cascade(label="File", menu=file)
edit = Menu(menubar, tearoff=0)
edit.add_command(label="Undo")
edit.add_separator()
edit.add_command(label="Cut")
edit.add_command(label="Copy")
edit.add_command(label="Paste")
menubar.add_cascade(label="Edit", menu=edit)
minimap = BooleanVar()
minimap.set(True)
darkmode = BooleanVar()
darkmode.set(False)
view = Menu(menubar, tearoff=0)
view.add_checkbutton(label="show minimap", onvalue=1, offvalue=0, variable=minimap)
view.add_checkbutton(label='Darkmode', onvalue=1, offvalue=0, variable=darkmode, command=darkMode)
menubar.add_cascade(label='View', menu=view)
help = Menu(menubar, tearoff=0)
help.add_command(label="About", command=about)
menubar.add_cascade(label="Help", menu=help)
ws.config(menu=menubar)
ws.mainloop()
Output:
In this output, the mini-map option is clicked which is why the map is appearing. deselecting the box will remove the mini-map. The dark mode checkbutton is not selected which is why the screen is normal. You can copy the code and try it by yourself.
Python Tkinter Menu bar Class
- Class plays important role in all programming languages.
- The class helps in avoiding repetition of code and facilitates us to use part of code for another program.
- A code to rotate a fan can be used in drones, ceiling fan, etc programs, it’s like to write once use anywhere.
- In this section, we will learn about creating a class for a menu bar
Code:
In this code, box with 3 menus has been created file, edit and help. The output is similar to previous section but the code is in a class format.
from tkinter import *
from tkinter import messagebox
class MenuBar(Menu):
def __init__(self, ws):
Menu.__init__(self, ws)
file = Menu(self, tearoff=False)
file.add_command(label="New")
file.add_command(label="Open")
file.add_command(label="Save")
file.add_command(label="Save as")
file.add_separator()
file.add_command(label="Exit", underline=1, command=self.quit)
self.add_cascade(label="File",underline=0, menu=file)
edit = Menu(self, tearoff=0)
edit.add_command(label="Undo")
edit.add_separator()
edit.add_command(label="Cut")
edit.add_command(label="Copy")
edit.add_command(label="Paste")
self.add_cascade(label="Edit", menu=edit)
help = Menu(self, tearoff=0)
help.add_command(label="About", command=self.about)
self.add_cascade(label="Help", menu=help)
def exit(self):
self.exit
def about(self):
messagebox.showinfo('PythonGuides', 'Python Guides aims at providing best practical tutorials')
class MenuDemo(Tk):
def __init__(self):
Tk.__init__(self)
menubar = MenuBar(self)
self.config(menu=menubar)
if __name__ == "__main__":
ws=MenuDemo()
ws.title('Python Guides')
ws.geometry('300x200')
ws.mainloop()
Output:
In this output, the menu bar with the option menus is displayed. Out of these Exit and About are functional.
Read: Python Tkinter Colors
Python Tkinter Menu bar icon
- This is not possible to add icons on the menu bar due to limitations.
- Tkinter outsources the menu bar from another vendor, due to which it provides limited features to the Tkinter menu bar.
- In the Tkinter menu bar color section we have seen that it was not allowed of mac and windows
- We did try to implement it on Linux, but it didn’t work over there as well.
- Software is a mysterious industry incase you find the solution please do share within the comments.
Python Tkinter Menu bar submenu
Now, let us see how to create a submenu in a Python Tkinter Menu bar.
- Submenu refers to a menu inside a menu.
- In other words, nested menu.
- Example: View > Ratio > option 1, option 2, option 3
- User clicks on the View menu, Ratio option appears now when the user clicks on Ratio then further 3 options appear. This is called the sub-menu.
Code:
from tkinter import *
from tkinter import messagebox
class MenuBar(Menu):
def __init__(self, ws):
Menu.__init__(self, ws)
file = Menu(self, tearoff=False)
file.add_command(label="New")
file.add_command(label="Open")
file.add_command(label="Save")
file.add_command(label="Save as")
file.add_separator()
file.add_command(label="Exit", underline=1, command=self.quit)
self.add_cascade(label="File",underline=0, menu=file)
edit = Menu(self, tearoff=0)
edit.add_command(label="Undo")
edit.add_separator()
edit.add_command(label="Cut")
edit.add_command(label="Copy")
edit.add_command(label="Paste")
self.add_cascade(label="Edit", menu=edit)
view = Menu(self, tearoff=0)
ratio = Menu(self, tearoff=0)
for aspected_ratio in ('4:3', '16:9'):
ratio.add_command(label=aspected_ratio)
view.add_cascade(label='Ratio', menu=ratio)
self.add_cascade(label='View', menu=view)
help = Menu(self, tearoff=0)
help.add_command(label="About", command=self.about)
self.add_cascade(label="Help", menu=help)
def exit(self):
self.exit
def about(self):
messagebox.showinfo('PythonGuides', 'Python Guides aims at providing best practical tutorials')
class MenuDemo(Tk):
def __init__(self):
Tk.__init__(self)
menubar = MenuBar(self)
self.config(menu=menubar)
if __name__ == "__main__":
ws=MenuDemo()
ws.title('Python Guides')
ws.geometry('300x200')
ws.mainloop()
Output:
In this output, View has menu option as Ratio, Ratio further has sub-menu.
You may like the following Python tutorials:
- Python Tkinter Checkbutton – How to use
- Python Tkinter radiobutton – How to use
- Python Tkinter Button – How to use
- Python Tkinter Entry – How to use
- Python tkinter label – How to use
- Python generate random number and string
- Python write list to file with examples
- Python Tkinter Progress bar
- Python Tkinter Exit Program
- How to Take User Input and Store in Variable using Python Tkinter
- How to read a text file using Python Tkinter
In this section we have learned about Python Tkinter menu bar, also we have covered these topics.
- Python Tkinter Menu bar
- Python Tkinter Menu bar color
- Python Tkinter Menu bar background-color
- Python Tkinter Menu bar Checkbox
- Python Tkinter Menu bar Class
- Python Tkinter Menu bar icon
- Python Tkinter Menu bar submenu
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.