About Lesson
In this TKinter Tutorial we are going to learn about creating TKinter Menu, we will lean how you can create menu, menubar and menu items in tkinter.
This is the complete code for this lesson.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
from tkinter import * class Window(Tk): def __init__(self): super(Window, self).__init__() self.title("Tkinter Menu") self.minsize(500,400) self.wm_iconbitmap("myicon.ico") self.create_menu() def create_menu(self): menuBar = Menu(self) self.config(menu = menuBar) file_menu = Menu(menuBar, tearoff = 0) menuBar.add_cascade(label = "File", menu = file_menu) file_menu.add_command(label = "New") file_menu.add_command(label = "Exit") file_menu.add_separator() file_menu.add_command(labe = "Open") help_menu = Menu(menuBar, tearoff = 0) menuBar.add_cascade(label = "Help", menu = help_menu) help_menu.add_command(label = "About") window = Window() window.mainloop() |
You can use this code for creating tkinter menu.
1 2 |
menuBar = Menu(self) self.config(menu = menuBar) |
In here we have created a file menu.
1 |
file_menu = Menu(menuBar, tearoff = 0) |
This is used for adding separator in your menu.
1 |
file_menu.add_separator() |
Using add_command() we can add menu items to the menubar.
1 |
file_menu.add_command(label = "New") |
Run the complete code and this is the result.