Press "Enter" to skip to content

How to Create Menu Bar Using Tkinter in Python?

The below is an example to show you how to create a menu bar using Tkinter in Python.

Python – Create Menu Bar Using Tkinter Example

In the following example, it will create a menu bar and will give the choices to the user to select. On selection, it will print on the screen and if the user will select the Quit option then it will exit the program.

import tkinter

def set_menu(window, choices):
    menubar = tkinter.Menu(root)

    def _set_choices(menu, choices):
        for label, command in choices.items():
            if isinstance(command, dict):
                # Submenu
                submenu = tkinter.Menu(menu)
                menu.add_cascade(label=label, menu=submenu)
                _set_choices(submenu, command)
            elif label == '-' and command == '-':
                # Separator
                # Simple choice
                menu.add_command(label=label, command=command)

    _set_choices(menubar, choices)

if __name__ == '__main__':
    import sys

    root = tkinter.Tk()

    from collections import OrderedDict

    set_menu(root, {
        'File': OrderedDict([
            ('Open', lambda: print('Open!')),
            ('Save', lambda: print('Save')),
            ('-', '-'),
            ('Quit', lambda: sys.exit(0))


Tkinter menu bar example.Open!

Vinish Kapoor

Hi, I am a full stack developer and writing about development. I document everything I learn and help thousands of people. is created, written, and maintained by me; it is built on WordPress, and hosted by Bluehost. Connect with me on Facebook, Twitter, GitHub, and get notifications for new posts.

Be First to Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *