How do you use buttons in Tkinter?

+2 alex constantin · December 14, 2014
Can someone guide me through how buttons are made and how to make the do functions when they're clicked?

Post a Reply

Replies

Oldest  Newest  Rating
+1 Simon Kvannli · December 20, 2014
Here you have a short code on the basics of a button. If you want to learn more about it i suggest googeling tkinter button tutorial or something, but this code should give you an idea of how it works:)

import tkinter
import tkinter.messagebox

class MyGUI:
    def __init__(self):

        #Create the main window widget
        self.main_window = tkinter.Tk()

        #Create a Button widget. The text "Click Me!"
        #should appear on the face of the Button. The
        #do_something method sould be executed when the
        #user clicks the Button

        self.my_button = tkinter.Button(self.main_window, \
                                        text="Click me!", \
                                        command = self.do_something)

        #Pack the button
        self.my_button.pack()

        #Enter the tkinter main loop
        tkinter.mainloop()

    #The do_something method is a callback function
    #for the Button widget

    def do_something(self):
        #Display an info dialog box
        tkinter.messagebox.showinfo("Response",\
                                    "Thanks for clicking the button")

my_gui = MyGUI()
  • 1

Python

106,995 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator