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 Name | Description |
---|---|
anchor | Controls 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 / bg | Sets the background color of widget, Default Is platform specific |
bitmap | Bitmap to be displayed on the widget. If the image option is specified, this option is ignored silently. |
borderwidth / bd | Width of buttons border. The default is system specific but usually 1 to 2 pixels. |
command | The 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. |
compound | Controls 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 |
cursor | The cursor to display when the mouse moves over the button. By default, the standard cursor is used. |
disabledforeground | The foreground color to use when the button is disabled or invalid. The default is system specific. |
font | Font to use for button. Buttons can only contain single font text. The default is system specific. |
foreground / fg | Sets the color for text or bitmap in button. The default is system specific |
height | The 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. |
image | Image to display on the widget. The value must be PhotoImage, BitmapImage, or compatible object. If specified, this overrides text and bitmap options. |
padx | Horizontal padding to add around the text. The default is 1 pixel. |
pady | Vertical padding to add around the text. The default is 1 pixel. |