Code in Python
Machine Learning and Deep Learning Blogs
search

How to Create Drop Down Menus in Tkinter

In this chapter, we will learn how to build a basic framework of a common piece of software and that includes, making the drop down menus. Every software we buy has menus at the top that drop down when clicked. We will also learn how to make a toolbar.

This is all built-in. we don’t have to do it from scratch. First of all, we have to create a basic function.

def printsomething():
   print("CIP is best for learning python:-P") 

First, we have to create this function and then we have to create the main window var.

var = Tk() 

Whenever we want to create an item in Tkinter we have to give a variable or an object name and in order to create a menu, the class for this is Menu. Inside menu, we have to enter where we want to put it. We have one window called var, so we will stick it in there.

main_menu = Menu(var) 

Now, we have menu object appearing in the main window. We have to configure to make sure that Tkinter recognizes it as the menu.

var.config(menu = main_menu) 

We have to tell which menu we want to configure. The menu is a parameter that is used to specify the menu we want to configure. Now, we want to make File menu. For that, we have to create a sub-menu, this sub-menu will be the thing that drops down.

sub_menu = Menu(main_menu) 

Inside menu, we have to enter where we want to put it. Since we are not putting it into the main menu, we are putting it inside another menu called main_menu, we will specify the menu in which we are entering. We want to add an item that has to drop down functionality. It is called cascading in Tkinter. The method used for this is add_cascade().

main_menu.add_cascade(label = "File") 

Inside add_cascade, we have to specify what label we want to show up on that menu. Since this is a cascade, we have to mention what is our sub-menu. So go ahead and enter the name you have used for the sub menu using the menu parameter. Our file menu is blank right now. So we have to add some command for the sub-menu to perform. For this, we use the add_command() method.

sub_menu.add_command(label = "blank page", command = printsomething) 

Inside add-command, we have to enter the label that will show up in the sub-menu and after that, state the function it will perform using command parameter that we have learned in the previous chapter. Remember, do not put parenthesis after the function name in command parameter. Let’s add another sub_menu in the main_menu. We can add as many menus inside a menu.

sub_menu.add_command(label = "open", command = printsomething) 

A separator is a line that separates different sub-menus. It differentiates the group that will perform a certain piece of work to the group that will perform work related to something else. To create a separator line, the object used is, add_separator()

sub_menu.add_separator() 

Final Code – Drop Down Menus

The fully working program for this is,

from tkinter import *
def printsomething():
   print("CIP is best for learning python :-P")
var = Tk()
main_menu = Menu(var)
var.config(menu = main_menu)
sub_menu = Menu(main_menu)
main_menu.add_cascade(label = "File")
sub_menu.add_command(label = "blank page", command = printsomething)
sub_menu.add_separator()
sub_menu.add_command(label = "open", command = printsomething)
var.mainloop() 

drop down menu

Rahul Choudhary
Follow me

Rahul Choudhary

Rahul is a Python enthusiast. He likes to explore whatever is new in Python world. Rahul is a car fanatic and a serial chiller. Follow him on Facebook and Twitter.
Rahul Choudhary
Follow me

Latest posts by Rahul Choudhary (see all)

13 Comments

  1. Just what I was searching for, regards for posting.

  2. Very good post. I will be experiencing a few of these issues as well..

  3. I just got done eating a platter of spaghetti before visiting your site. It sure makes the full feeling all that much better.

  4. I love this site – its so usefull and helpfull.

  5. Howdy just wanted to give you a quick heads up and let you know a few of the pictures aren’t loading properly.
    I’m not sure why but I think its a linking issue.
    I’ve tried it in two different web browsers and both show the
    same results.

  6. Hi there, I log on to your blog daily. Your humoristic style
    is witty, keep it up!

  7. I blog often and I truly thank you for your content.

    This article has truly peaked my interest. I will bookmark
    your blog and keep checking for new information about once per
    week. I opted in for your RSS feed too.

  8. I like the helpful info you provide in your articles. I’ll bookmark your blog
    and check again here regularly. I’m quite sure I’ll learn a lot of new stuff right here!
    Good luck for the next!

  9. Have you ever considered about adding a little bit more than just your articles?
    I mean, what you say is fundamental and everything.
    Nevertheless just imagine if you added some great photos or video clips to give your posts more, “pop”!
    Your content is excellent but with images and videos, this blog could undeniably be one
    of the greatest in its niche. Wonderful blog!

  10. Have you ever considered writing an ebook or guest authoring on other sites?
    I have a blog centered on the same topics you discuss and would love to
    have you share some stories/information. I know my subscribers would appreciate your work.
    If you’re even remotely interested, feel free to shoot me an email.

  11. anything totally, however this paragraph offers nihe understanding yet.

Leave a Reply

Your email address will not be published.

*

© 2018 Code in Python