Tkinter Button Widget

Tkinter Button Widget

In this tutorial, you’ll learn about the Tkinter Button widget and how to use it to create various kinds of buttons.

Introduction to the Tkinter Button Widget

Button widgets represent a clickable item in Tkinter applications. They can display text or images to indicate the action performed when clicked.

Buttons can also display text in a single font and support multiple lines. Additionally, you can underline characters to indicate keyboard shortcuts.

To invoke a function or method when the button is clicked, assign its command option to the desired function. This is known as command binding.

Creating a Button

To create a button, use the ttk.Button constructor:

button = ttk.Button(master, **options)
Code copied!

Button Options

The typical syntax for creating a button is:

button = ttk.Button(master, text="Button Label", command=callback_function)
Code copied!

In this syntax:

  • master: The parent widget where the button is placed.
  • text: The label displayed on the button.
  • command: The callback function invoked when the button is clicked.

Command Callback

The command option links the button’s action to a function. Here’s how to assign a callback:

def callback():
    print("Button clicked!")

ttk.Button(root, text="Demo Button", command=callback)
Code copied!

You can also use a lambda expression for simple callbacks:

ttk.Button(root, text="Demo Button", command=lambda: print("Button clicked!"))
Code copied!

Button States

Buttons can have different states:

  • Normal: The button is clickable.
  • Disabled: The button is greyed out and unresponsive.

Control the state using the state() method:

button.state(['disabled'])  # Disable the button
button.state(['!disabled'])   # Enable the button
Code copied!

Examples of Tkinter Buttons

1) Simple Exit Button Example

The following program demonstrates how to create an Exit button that terminates the application:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
root.geometry('300x200')
root.title('Button Demo')

exit_button = ttk.Button(root, text='Exit', command=root.quit)
exit_button.pack(ipadx=5, ipady=5, expand=True)

root.mainloop()
Code copied!

2) Image Button Example

This example shows how to create a button with an image:

import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import showinfo

root = tk.Tk()
root.geometry('300x200')
root.title('Image Button Demo')

def download_clicked():
    showinfo(title='Information', message='Download button clicked!')

download_icon = tk.PhotoImage(file='./path_to_image/download.png')
download_button = ttk.Button(root, image=download_icon, command=download_clicked)
download_button.pack(ipadx=5, ipady=5, expand=True)

root.mainloop()
Code copied!

3) Button with Text and Image

To display both text and image on a button, use the compound option:

import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import showinfo

root = tk.Tk()
root.geometry('300x200')
root.title('Image Button Demo')

def download_clicked():
    showinfo(title='Information', message='Download button clicked!')

download_icon = tk.PhotoImage(file='./path_to_image/download.png')
download_button = ttk.Button(root, image=download_icon, text='Download', compound=tk.LEFT, command=download_clicked)
download_button.pack(ipadx=5, ipady=5, expand=True)

root.mainloop()
Code copied!

Conclusion

In this tutorial, you learned how to use the Tkinter Button widget to create interactive buttons in your applications. Key takeaways include:

  • Using the ttk.Button() class to create buttons.
  • Assigning a callback function to the command option for handling button clicks.
  • Utilizing tk.PhotoImage() to display images on buttons.
  • Combining text and images using the compound option.