Code in Python
Machine Learning and Deep Learning Blogs
search

How to implement Mouse Click Event in Tkinter

Whenever we use Tkinter, we are not just limited to bind one function to one widget. With widgets, we can bind multiple events to it depending on what we do with that widget, it behaves in different kind of ways. We can have a button, and if we left click it, something happens and if you right click it, something else happens.

Our goal in this article is to do something when one of the mouse button is clicked. That something is called mouse click event. The task here is to build a widget and if we left click, middle click or right click it, different things are going to happen. There will be three different functions for this.

def leftclick(event):
    print("left")
def middleclick(event):
    print("middle")
def rightclick(event):
    print("right") 

Instead of having a button, we will build a basic frame. It’s going to look like just an empty window but it will be an invisible frame.

frame = Frame(var, width=300, height=250)

We can specify width and height of a window by using two simple keywords, width, and height. Now, we will bind the functions to the frame(widget).

frame.bind("<Button-1>", leftclick)
frame.bind("<Button-2>", middleclick)
frame.bind("<Button-3>", rightclick) 

Now, whenever we left click on the mouse, function leftclick will be called. Same goes for the other two functions.
The only thing left to do is to pack the frame. Without packing, the frame will not show up.

frame.pack() 

In this chapter, we have learned how to add multiple functions to a widget.

Final Mouse Click Event Code

The whole program will look like,

from tkinter import *
var = Tk()
def leftclick(event):
    print("left")
def middleclick(event):
    print("middle")
def rightclick(event):
    print("right")
frame = Frame(var, width=300, height=250)
frame.bind("<Button-1>", leftclick)
frame.bind("<Button-2>", middleclick)
frame.bind("<Button-3>", rightclick)
frame.pack()
var.mainloop() 

So far until now, we have learned how to implement mouse click events, how to bind functions, make frames and structure our layout. Read more of our articles to Tkinter and Python. Share it with your friends if you like our article.

mouse click event

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)

12 Comments

  1. If some one wants expert view about blogging then i recommend him/her to visit this weblog, Keep up the good
    work.

  2. Hi! Do you know if they make any plugins to help with Search Engine Optimization? I’m
    trying to get my blog to rank for some targeted keywords but I’m not
    seeing very good success. If you know of any please share.
    Kudos!

  3. Excellent post. I was checking constantly this blog and I’m impressed!
    Extremely helpful info specifically the last part :
    ) I care for such info a lot. I was seeking this certain info
    for a long time. Thank you and best of luck.

  4. Unquestionably imagine that which you stated.
    Your favorite justification appeared to be at the net the
    simplest factor to take into account of. I say to
    you, I certainly get annoyed whilst other folks consider concerns that they
    just do not understand about. You managed to hit the nail
    upon the top as smartly as defined out the
    whole thing with no need side-effects , people could take a signal.
    Will likely be back to get more. Thanks

  5. Hello, just wanted to tell you, I enjoyed this blog post.
    It was helpful. Keep on posting!

  6. Hello there! This post couldn’t be written much better!
    Going through this post reminds me of my previous roommate!

    He always kept preaching about this. I most certainly will send this post
    to him. Pretty sure he will have a very good read.
    Many thanks for sharing!

  7. It is appropriate time to make some plans for the future and it is time to be happy.

    I’ve read this post and if I could I desire to suggest you some interesting things or suggestions.
    Maybe you can write next articles referring to this article.
    I desire to read more things about it!

  8. Attractive portion of content. I just stumbled upon your website and in accession capital to claim that I
    acquire actually enjoyed account your blog
    posts. Anyway I will be subscribing to your augment and even I fulfillment you get
    right of entry to persistently quickly.

  9. I really like what you guys are usually up too. This kind of clever work and coverage!
    Keep up the great works guys I’ve added you guys to our blogroll.

  10. I precisely wished to thank you very much once more. I am not sure the things that I would’ve implemented in the absence of those methods contributed by you regarding my problem. It had become a difficult condition for me, however , taking note of this well-written avenue you treated the issue made me to cry for contentment. Extremely grateful for the guidance and in addition pray you are aware of an amazing job that you’re putting in educating many others through the use of your web site. Most likely you haven’t encountered all of us.

Leave a Reply

Your email address will not be published.

*

© 2018 Code in Python