Code in Python
Machine Learning and Deep Learning Blogs
search

How to bind functions to Tkinter layout

We know how to make a layout in a couple of different ways. We know how to organize our widgets around our screen. Let’s learn how to make GUI actually interact with our computer program.

In this chapter, we will learn how to bind functions to widgets. In other words, how we can do something like click a button and they call a function from our computer program.
Let’s make a basic function.

def Printname():
  print("hello, I like programming in python") 

Now, we want to call this function whenever we click a button. So the first thing we need is a button.

button_1 = Button(var, text = "print", command = Printname)
button_1.pack() 

This button is using a new parameter called command. Command runs a certain function whenever the button is clicked. Mention the name of the function in front of the command parameter to run the specific program. Whenever we use it, we don’t use parenthesis after the name of the function.

The whole program is like,

from tkinter import *
var = Tk()
def Printname():
  print("hello, I like programming in python")
button_1 = Button(var, text = "print", command = Printname)
button_1.pack()
var.mainloop() 

There is another way to which we can bind functions to widgets. We don’t need command anymore for this technique.
In the parenthesis after the function-name, we are going to pass an event.


def Printname(event):
  print("hello, I like programming in python") 

An event to our computer is something that occurs. For eg., button click, scroll, mouse movement, pressing a key on the keyboard. All these are called events (anything which user can do). Whenever a specific event occurs, then call some function accordingly.

To bind the function to the button, we will call a function called bind. Bind takes two parameters. firstly, What event are you waiting for to occur and Secondly, What function you want to occur.

The event name for left clicking our mouse button is <Button-1>. <Button-2> for middle mouse button and <Button-3> for right mouse button.

button_1.bind("<Button-1>", Printname) 

Remember, whenever you call a function do not add parenthesis.

Final Code – Binding Functions

The whole program is like,

from tkinter import *
var = Tk()
def Printname(event):
print("hello, I like programming in python")
button_1 = Button(var, text = "print")
button_1.bind("<Button-1>", Printname)
button_1.pack()
var.mainloop() 

How to bind functions

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)

14 Comments

  1. It’s not my first time to pay a quick visit this site, i am visiting this site dailly and get nice facts from here every day.

  2. You can certainly see your enthusiasm within the work you
    write. The arena hopes for even more passionate writers such as you who aren’t afraid to say
    how they believe. At all times follow your heart.

  3. I’m gone to tell my lіttle brother, that he should also рay a viѕit this
    webpаge on regular basis to obtain սpdated from most recent news.

  4. I quite like looking through an article that will make men and women think.

    Also, thank you for allowing for me to comment!

  5. I like the helpful info you provide in your articles.
    I’ll bookmark your weblog and check again here frequently.

    I’m quite sure I will learn many new stuff right here!
    Good luck for the next!

  6. Usually I do not learn article on blogs, however I wish to say that
    this write-up very pressured me to try and do so! Your writing style
    has been amazed me. Thanks, quite great article.

  7. This is very interesting, You’re a very skilled blogger.
    I’ve joined your rss feed and look forward to seeking more of your excellent post.
    Also, I have shared your web site in my social networks!

  8. Right away I am going to do my breakfast, later than having my breakfast coming over again to read other news.

  9. Its like you learn my mind! You appear to know a lot
    about this, like you wrote the e-book in it or something.

    I think that you simply could do with some percent to power the message home a little bit, but other than that, that is
    fantastic blog. An excellent read. I’ll definitely be back.

  10. Please let me know if you’re looking for a article writer for your site. You have some really good articles and I believe I would be a good asset. If you ever want to take some of the load off, I’d absolutely love to write some material for your blog in exchange for a link back to mine. Please send me an e-mail if interested. Kudos!

  11. My spouse and I absolutely love your blog and find most of your post’s to be what precisely I’m looking for.
    Do you offer guest writers to write content available for
    you? I wouldn’t mind producing a post or elaborating on many of
    the subjects you write in relation to here. Again, awesome web site!

  12. Hi there, I discovered your website by means of Google at the same time
    as searching for a related matter, your web site
    came up, it seems great. I’ve bookmarked it in my google bookmarks.

    Hello there, just was alert to your blog via Google, and found that it’s really informative.
    I’m gonna watch out for brussels. I will be grateful in case you continue this in future.
    A lot of folks will likely be benefited out of your writing.
    Cheers!

  13. I got this web site from my buddy who informed
    me concerning this web page and at the moment this time I am browsing this website and reading very
    informative articles or reviews here.

  14. I really like what you guys are up too. This sort of clever work and
    coverage! Keep up the terrific works guys I’ve incorporated you guys to my blogroll.

Leave a Reply

Your email address will not be published.

*

© 2018 Code in Python