Tkinter Button

Tkinter Button

Tkinter Button is an widget used to add buttons in python applications. These buttons can perform different actions according to requirement . These buttons can display images instead of plain text.

Tkinter also allows us to format these buttons accordingly (using colors , fonts) . Each button has a specific function , which calls automatically when button is clicked . Adding button into python application is very easy task .

Syntax

Button( MainWindow , OtherParameters..... )

Parameters

  • MainWindow : The main window is a master window , in which all the widgets are placed. We can say it is a container , that holds all the content of the application.
  • OtherParameters :  We can add multiple parameters to an button. Parameters are the additional information or characteristics that we can add to a function . We can add button’s color , background-color , height etc. Also , we can pass an action ( which function performs on click ) as an parameter.

Example 1 – A simple button

from tkinter import *
win = Tk()
btn = Button(win , text = "Button" , command = quit)
btn.pack()
win.mainloop()

Output

tkinter button
tkinter button

Explanation

  • In the above code , we have create an main window in line 2
  • line 3 is responsible for creating button , in this we have use Button() widget of tkinter
  • Here we pass 3 parameters to button ,first is main window. Second one is text to be display on button.
  • Third parameter pack() organizes the widgets in blocks.
  •  The last line is actual responsive to display output on the screen.

If you want to learn more about pack() than you can visit the link blow:

pack()

Example 2 – Tkinter Button using OOP

from tkinter import *
from tkinter import messagebox

class mybutton:
    def __init__(self, masterWindow):
        frame = Frame(masterWindow)
        frame.pack()
        self.button = Button(frame, text = "Exit" , bg= 'red',fg = 'white',command= quit,pady=5, padx=20, highlightcolor='gray')
        self.button.pack(side=LEFT)

        self.Click = Button(frame ,  bg = 'green', command=self.btn_action,  text="Click me",font='arial')
        self.Click.pack(side=RIGHT)

    def btn_action(self):
        print("Button Clicked!!!")
        messagebox.showinfo("Greetings","Thanks for click on the button")
root = Tk()
obj = mybutton(root)
root.mainloop()

Output

tkinter button
tkinter button

Explanation

In code above , there are two buttons:

  1. Exit :  It uses built in quit command and closes window when clicked .
  2. Click Me :  It returns an pop-up messagebox with some text and OK button . Also it prints “Button Clicked” into console. For this we have created function , btn_function()  within class mybutton .

So , this is all about creating button with some action in Tkinter . I hope you guys enjoyed the article !! Thanks!!!

30 thoughts on “Tkinter Button

Leave a Reply

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

  1. Quite a few online communities include singles sets which attempt enjoyable click here activities mutually, all this is actually is a good renewable dating approach. Incidents similar to pedaling, bowling, curling, video clip evenings, breaking a leg and comedy club sets tend to be organized with the singles group, but it enables a evenly-distributed group of objectives to have a great as well as laid-back nights. Together with emphasis apply to the actual activity themselves as an alternative to building a romantic connection, you will need plenty of demand journey singles in addition to sights manifest additional the natural way throughout this kind of environment.

  2. I just could not depart your site before suggesting that I really enjoyed the standard information a person provide for your visitors? Is gonna be back often to check up on new posts

  3. I want to show my thanks to the writer just for bailing me out of this type of trouble. As a result of browsing through the the web and seeing solutions that were not beneficial, I believed my life was over. Existing devoid of the strategies to the issues you have resolved through your good blog post is a critical case, as well as the ones which could have adversely damaged my career if I hadn’t encountered your web page. Your own training and kindness in taking care of all the pieces was helpful. I’m not sure what I would have done if I had not discovered such a subject like this. I can also at this moment relish my future. Thanks so much for this reliable and amazing guide. I won’t think twice to propose your blog to anybody who needs and wants guidance on this matter.

  4. What i do not realize is in reality how you’re now not really much more neatly-favored than you might be now. You are so intelligent. You recognize therefore considerably in terms of this topic, made me for my part believe it from so many various angles. Its like men and women are not involved until it¡¦s something to accomplish with Woman gaga! Your own stuffs excellent. Always deal with it up!

  5. you’re in reality a good webmaster. The web site loading velocity is incredible. It seems that you’re doing any distinctive trick. In addition, The contents are masterwork. you have done a excellent job in this subject!

  6. I just couldn’t leave your web site before suggesting that I extremely enjoyed the usual information an individual supply on your visitors? Is going to be back ceaselessly to investigate cross-check new posts

  7. Thanks a lot for sharing this with all people you actually recognize what you are speaking approximately! Bookmarked. Please also discuss with my web site =). We will have a hyperlink change arrangement among us!

  8. I just want to say I am just newbie to blogging and site-building and seriously enjoyed your web blog. Most likely I’m planning to bookmark your website . You amazingly have amazing stories. Cheers for sharing your web page.

  9. Very nice post. I just stumbled upon your weblog and wanted to say that I have truly enjoyed surfing around your blog posts. After all I’ll be subscribing to your feed and I hope you write again soon!

  10. Wonderful goods from you, man. I have understand your stuff previous to and you’re just too magnificent. I actually like what you’ve acquired here, really like what you are stating and the way in which you say it. You make it entertaining and you still take care of to keep it wise. I can’t wait to read far more from you. This is really a great site.

  11. I think this is one of the most important information for me. And i’m glad reading your article. But should remark on few general things, The web site style is perfect, the articles is really excellent : D. Good job, cheers

  12. Its like you read my thoughts! You appear to understand a lot about this, such as you wrote the book in it or something.

    I feel that you simply could do with a few % to drive the message house a little
    bit, however other than that, that is fantastic blog.
    A fantastic read. I’ll certainly be back.

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.