button images in tkinter with code examples

Button images can add a lot of visual appeal to a graphical user interface. In tkinter, the most popular GUI library for Python, creating button images is incredibly easy. Let's take a look at how we can create button images in tkinter with code examples.

Creating Image Buttons with tkinter

To use image buttons in tkinter, we first need to import the necessary libraries:

import tkinter as tk
from tkinter import PhotoImage

Next, we create a tkinter window object:

root = tk.Tk()

Now, we can create image objects using the PhotoImage class. We can provide a path to an image file or simply pass in the image data directly as a string:

image = PhotoImage(file="button.gif")

The file argument is used to specify the path to the image file. Here, we're assuming that we have a file named "button.gif" in the same directory as our Python script.

Alternatively, we can pass in the image data as a string. For example, if we have a PNG image named "button.png" in the same directory as our script, we could load it like this:

image = PhotoImage(data=image_data)

Once we have our image object, we can create a button using the Button class. We can use the image argument to specify the image to use for the button:

button = tk.Button(root, image=image)

Next, we need to call the pack method on the button to add it to our tkinter window:


Now, when we run our code, we should see an image button displayed on our tkinter window.

Adding Button Click Events

Of course, we typically want our buttons to do something when we click on them. To add a click event to our image button, we simply need to pass a function as the value of the command argument:

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

button = tk.Button(root, image=image, command=on_button_click)

In this example, we've defined a function named on_button_click that simply prints out a message to the console. We then pass this function as the value of the command argument when creating our button.

Now, when we click on the image button, our on_button_click function will be called, and "Button clicked!" will be printed to the console.


Image buttons are a great way to add some visual flair to your tkinter GUIs. With just a few lines of code, you can create image buttons that respond to user clicks. Whether you're building a simple game or a full-fledged application, image buttons can help make your tkinter UIs more engaging and intuitive.

here are some additional details about creating image buttons in tkinter.

Image Formats for Image Buttons

When loading image files for image buttons in tkinter, there are some limitations on which file formats can be used. The supported image formats are determined by the underlying graphics library used by tkinter. Most common image formats, such as PNG, GIF, and JPEG, are safe to use with tkinter, but a few less common formats may not work.

To be on the safe side, it's a good idea to stick to commonly used image formats when creating image buttons in tkinter.

Button States

Another important aspect of creating Image Buttons in tkinter is button states. By default, a tkinter button will have three separate states: normal, active, and disabled. Each of these states can have its own image.

For example, we can create separate images for a button in its "normal" and "active" states:

normal_image = PhotoImage(file="normal.gif")
active_image = PhotoImage(file="active.gif")

We can then use these images to create a button, but instead of passing in a single image, we pass in a dictionary of images, with keys corresponding to the various states:

button_images = {"normal": normal_image, "active": active_image}
button = tk.Button(root, image=button_images)

When the button is in its "normal" state, the normal_image will be used. When the button is clicked and enters its "active" state, the active_image will be used.

Customizing Image Buttons with Styles

tkinter's ttk module provides additional options for customizing button widgets. We can use a ttk.Style object to customize the appearance of our image buttons.

For example, to change the background color of a button, we can define a style object, change the background option, then apply the style to our button:

style = ttk.Style()
style.configure("Custom.TButton", background="blue")
button = ttk.Button(root, style="Custom.TButton", image=image)

In this example, we've defined a style named "Custom.TButton" and set its background color to blue. We then apply the style to our button by passing in the style argument.


Image buttons are a visually compelling way to add interactivity to tkinter GUIs. In this article, we learned how to create image buttons using tkinter's PhotoImage class. We also explored additional options for customizing and styling image buttons. By experimenting with these features, we can create polished GUIs that are both intuitive and engaging.

Popular questions

  1. What is the main library used for GUI creation in Python?
  • The main library used for GUI creation in Python is tkinter.
  1. How do we import the necessary libraries for working with button images in tkinter?
  • We can import the necessary libraries using the following code:
import tkinter as tk
from tkinter import PhotoImage
  1. Can we use any image format when creating image buttons in tkinter?
  • No, we need to stick to commonly used image formats such as PNG, GIF, and JPEG when creating image buttons in tkinter.
  1. How can we customize the appearance of button widgets in tkinter?
  • We can use tkinter's ttk module and the ttk.Style object to customize the appearance of button widgets.
  1. What is the purpose of the command argument when creating buttons in tkinter?
  • The command argument when creating buttons in tkinter is used to specify the function to call when the button is clicked.



As a developer, I have experience in full-stack web application development, and I'm passionate about utilizing innovative design strategies and cutting-edge technologies to develop distributed web applications and services. My areas of interest extend to IoT, Blockchain, Cloud, and Virtualization technologies, and I have a proficiency in building efficient Cloud Native Big Data applications. Throughout my academic projects and industry experiences, I have worked with various programming languages such as Go, Python, Ruby, and Elixir/Erlang. My diverse skillset allows me to approach problems from different angles and implement effective solutions. Above all, I value the opportunity to learn and grow in a dynamic environment. I believe that the eagerness to learn is crucial in developing oneself, and I strive to work with the best in order to bring out the best in myself.
Posts created 2374

Leave a Reply

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

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top