What is Button Widget?

A button is the standard Tkinter widget class for implementing different kinds of buttons on GUI form a button can contain text and images. 

Example

The following Python script creates a window that contains one button with text “Say, Hello”. One method (sayHello) is associated with this button using the command option. Whenever the user clicks on the button method sayHello invoked.

You need to write following code in any text editor and then save it with .py extension. To execute the application type py ButtonExample.py command in terminal.

from tkinter import *

# Top level window 
window = Tk()

window.title("Studyfied.com")
window.geometry('350x200')

# Python function associated with 'button'
def sayHello():
    print("Hello!")

# A button
button = Button(window, text="Say, Hello", command=sayHello)
button.pack()

window.mainloop()

Output

The above code produces the following output in windows operating system.

Explanation

Following line creates a Tkinter window

from tkinter import *

window = Tk()

window.title("Studyfied.com")
window.geometry('350x200')

See explanation of Tkinter window – Tkinter top level window


It is a python function which is associated with our button. Whenever function gets called it prints ‘Hello!’ message on the terminal.

# Python function associated with 'button'
def sayHello():
    print("Hello!")

This snippet creates a button widget.

# A button
button = Button(window, text="Say, Hello", command=sayHello)
button.pack()

Properties

  • window – The container window.
  • text – Text to be displayed on the button widget.
  • command – Method associated with the button action event.

The second line button.pack(), is geometry manager that packs widgets into rows or columns. 

Some button properties

Property NameDescription
anchorControls the position of the text (or image) within the button. Valid values: N, NE, E, SE, S, SW, W, NW, or CENTER. Default is CENTER.
background / bgSets the background color of widget, Default Is platform specific
bitmapBitmap to be displayed on the widget. If the image option is specified, this option is ignored silently.
borderwidth / bdWidth of buttons border. The default is system specific but usually 1 to 2 pixels.
commandThe function or method called when the button is pressed. A callback is a function, a bound method, or any other callable Python object. If you do not use this option, nothing happens when the user presses the button.
compoundControls how text and images within a button are combined. By default, if an image or bitmap is given it will be drawn instead of text. When this option is set to CENTER, text is drawn on the image. Valid values: BOTTOM, LEFT, RIGHT, or TOP
cursorThe cursor to display when the mouse moves over the button. By default, the standard cursor is used.
disabledforegroundThe foreground color to use when the button is disabled or invalid. The default is system specific.
fontFont to use for button. Buttons can only contain single font text. The default is system specific.
foreground / fgSets the color for text or bitmap in button. The default is system specific
heightThe height of the button. If text is displayed on the button, the size is indicated in text units. If an image is displayed on the button, the size is specified in pixels (or screen units). If you set the size to 0 or omit it, it is calculated based on the contents of the label.
imageImage to display on the widget. The value must be PhotoImage, BitmapImage, or compatible object. If specified, this overrides text and bitmap options.
padxHorizontal padding to add around the text. The default is 1 pixel.
padyVertical padding to add around the text. The default is 1 pixel.