How to create buttons with images in Tkinter Python

share link

by vsasikalabe dot icon Updated: Jul 7, 2023

technology logo
technology logo

Solution Kit Solution Kit  

We must import ImageTk from the Python Pillow (PIL) package to display an image. The PhotoImage or BitmapImage objects are displayed in a widget. The PhotoImage(img) class displays the true color icons of images. The subprocess module creates new processes. It connects them to their input/output/error pipes. We can use the PIL module of Python by importing its ImageTk. The Image class adds images to a GUI screen. The Syntax for importing Image TK is (from PIL import ImageTk, Image).  


In Python, we can call a function using a tkinter button with the command option of button (). This has been completed using the Tkinter PhotoImage Class. It takes an image's file path and creates an object from it. This can be used with the other GUI elements. We need its PIL module linked in Python and its imagetk class to display images on Tkinter GUI. The PhotoImage Tkinter module enables the pictures to display in the language. The PhotoImage attribute in the image property of widgets has done this.  


We have to make a Tk root widget to start the Tkinter. This is a window with a title. There can only be one root widget that has to be created before any other widgets.root = tk.Tk(). Next, we have to add the path of the image location. The Opens and identifies the given path of the image file. Resizing means without cropping or cutting the image, making it smaller or larger. An image's dimensions will affect its file size and quality.  


Tkinter Photoimage is used to link user-defined images to the application. It's also the keyword called the image file and is stored in the path. The pack() fill option displays a widget to fill the entire frame. If the user expands the frame, the pack() expand option is to expand the widget. By default, fill options: NONE, which will keep the widget's original size. X.  


If the user clicks on the image, it will display the calculator. When we want to run the root of the application mainloop() method is executed on the main window. This method will not end, waiting for the user to click the event until the user exits the program.  


The Syntax for importing tkinter is (import tkinter as tk). The button text is updated if the variable is changed. We are creating a button using the Button function. We can avoid using the global button. Always keep a reference to the image on the button object. To make an ordinary button look like it is held down. You can change the relief from RAISED to SUNKEN if you wish to implement a toolbox. We can pass the file path of the image you want to import into the PhotoImage class.  


The grayscale method has been the base64-encoded format of the gif, jpg. This library must support the file formats like PPM, PNG, GIF, JPG, JPEG, and BMP. These formats are included with the help of UI widgets like Label.  


You can link a Python function with it. When the button is pressed, the corresponding function or method will proceed. You can set the button's size through the height and width options. If you're displaying text, they define the Button size in text units. If you display a bitmap or an image, they define the Button size in pixels.  


Here is an example of how to create buttons with images in Tkinter Python: