Thursday, October 2, 2025
HomejQuery8 Examples to Be taught Python Tkinter Menu

8 Examples to Be taught Python Tkinter Menu


On this tutorial, we’ll present you easy methods to create menus within the Python purposes by tkitner library.

First, the examples will clarify easy methods to add a menu. Then we’ll add menu gadgets as a consumer pulls/clicks on the menu.

Later, we will even present easy methods to add sub menus inside the principle menu.

Actions as you click on on the menu merchandise will even be lined within the examples under.

w = Menu(prime, choices)

Allow us to begin with a easy instance of making a fundamental menu with a single merchandise.

We are going to create “File” fundamental menu after which it is going to comprise one merchandise “Open”.

As you open and click on on the Open menu merchandise, it closes the window.

Take a look on the code and output:



Output:

tkinter-menu-simple

Allow us to transfer forward and add three gadgets to the “File” menu:

Code:



Output:

tkinter-menu-items

We are going to add a separator between every menu merchandise (New, Open, and Save) for the demo solely.

So as to add a separator, use the add_separator() technique of the menu widget.

All gadgets merely shut the window as you click on on any:

Code:



Output:

tkinter-menu-seperator

An instance of a number of fundamental menu

Now we’ll create extra fundamental menus like File within the above examples.

In this system under, we’ve

  • File
  • Edit
  • Format
  • View
  • Assist

These are the principle gadgets. Upon clicking on any will open its personal menu. However first solely the principle menus:

Program:



Output:

tkinter-menu-multiple

On this instance, we’ll add menu gadgets for every fundamental menu.

Python program:



Output:

tkinter-menu-complete

In case your menu is meant to be multi-level i.e. sub-menu then you are able to do this simply through the use of tkinter menu widget.

For instance, in our above menu, we need to add the “Zoom In” and “Zoom Out” sum menu gadgets to the “Zoom” menu merchandise. That is how we are able to do that:

Code:



Output:

The menu widget has choices to provide background and foreground colours to the menu.

  • Through the use of bg possibility, chances are you’ll specify the background shade.
  • The fg possibility is used to paint the textual content of the menu gadgets.

See the instance under the place we added each choices. In fg, we supplied the colour identify and within the bg possibility we used the Hex worth of the colour:

Code:



Output:

tkinter-menu-colors

To face out a menu merchandise from others when the mouse hovers over that merchandise, use the next two choices of the menu widget:

  • activebackground – to set the background shade as mouse hovers
  • activeforeground – set the textual content shade

See an instance under the place we set these choices for all three menu gadgets.

Code:



Output:

tkinter-menu-colors-act

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments