The Wayback Machine - https://web.archive.org/web/20240812045030/https://www.geeksforgeeks.org/python-add-image-on-a-tkinter-button/
Open In App

Python | Add image on a Tkinter button

Last Updated : 25 Apr, 2019
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Tkinter is a Python module which is used to create GUI (Graphical User Interface) applications with the help of varieties of widgets and functions. Like any other GUI module it also supports images i.e you can use images in the application to make it more attractive.

Image can be added with the help of PhotoImage() method. This is a Tkinter method which means you don’t have to import any other module in order to use it.

Important: If both image and text are given on Button, the text will be dominated and only image will appear on the Button. But if you want to show both image and text then you have to pass compound in button options.

Button(master, text = "Button", image = "image.png", compound=LEFT)

compound = LEFT -> image will be at left side of the button
compound = RIGHT -> image will be at right side of button
compound = TOP -> image will be at top of button
compound = BOTTOM -> image will be at bottom of button

Syntax:

photo = PhotoImage(file = "path_of_file")

path_of_file is any valid path available on your local machine.

Code #1:




# importing only those functions
# which are needed
from tkinter import * 
from tkinter.ttk import *
  
# creating tkinter window
root = Tk()
  
# Adding widgets to the root window
Label(root, text = 'GeeksforGeeks', font =(
  'Verdana', 15)).pack(side = TOP, pady = 10)
  
# Creating a photoimage object to use image
photo = PhotoImage(file = r"C:\Gfg\circle.png")
  
# here, image option is used to
# set image on button
Button(root, text = 'Click Me !', image = photo).pack(side = TOP)
  
mainloop()


Output:

In output observe that only image is shown on the button and the size of the button is also bigger than the usual size it is because we haven’t set the size of the image.
 
Code #2: To show both image and text on Button.




# importing only those functions
# which are needed
from tkinter import * 
from tkinter.ttk import *
  
# creating tkinter window
root = Tk()
  
# Adding widgets to the root window
Label(root, text = 'GeeksforGeeks', font =(
  'Verdana', 15)).pack(side = TOP, pady = 10)
  
# Creating a photoimage object to use image
photo = PhotoImage(file = r"C:\Gfg\circle.png")
  
# Resizing image to fit on button
photoimage = photo.subsample(3, 3)
  
# here, image option is used to
# set image on button
# compound option is used to align
# image on LEFT side of button
Button(root, text = 'Click Me !', image = photoimage,
                    compound = LEFT).pack(side = TOP)
  
mainloop()


Output:

Observe that both text and image are appearing as well as size of the image is also small.



Previous Article
Next Article

Similar Reads

Python | Add style to tkinter button
Tkinter is a Python standard library that is used to create GUI (Graphical User Interface) applications. It is one of the most commonly used packages of Python. Tkinter supports both traditional and modern graphics support with the help of Tk themed widgets. All the widgets that Tkinter also has available in tkinter.ttk.Adding style in a tkinter.tt
4 min read
How to add a border color to a button in Tkinter?
In this article, we will learn how to add border color to a button in Tkinter. In the first example, we are using the frame widget to add border color to a button in frame by highlighting the border with black color and a thickness of 2. Example 1: Using Frame widget to add border color to a button. Steps: A frame is a container for widgets. In thi
2 min read
Difference Between The Widgets Of Tkinter And Tkinter.Ttk In Python
Python offers a variety of GUI (Graphical User Interface) frameworks to develop desktop applications. Among these, Tkinter is the standard Python interface to the Tk GUI toolkit. Tkinter also includes the 'ttk' module, which enhances the visual appeal and functionality of the applications. In this article, we will cover the differences, providing a
4 min read
How To Add An Image In Tkinter?
Python Tkinter supports various image formats such as PNG, JPEG, GIF, and BMP. Images are handled in Tkinter using the PhotoImage and BitmapImage classes and the Pillow Library. The PhotoImage class is more commonly used due to its support for color images, whereas BitmapImage is limited to monochrome bitmaps. Adding images in Tkinter can be done b
4 min read
Open a new Window with a button in Python-Tkinter
Python provides a variety of GUI (Graphic User Interface) such as PyQt, Tkinter, Kivy and soon. Among them, tkinter is the most commonly used GUI module in Python since it is simple and easy to learn and implement as well. The word Tkinter comes from the tk interface. The tkinter module is available in Python standard library.Note: For more informa
3 min read
How to create a pop up message when a button is pressed in Python - Tkinter?
In this article, we will see how to create a button and how to show a popup message when a button is pressed in Python. Tkinter is a standard Python Package for creating GUI applications. Tkinter may be a set of wrappers that implement the Tk widgets as Python classes. Python, when combined with Tkinter, provides a quick and straightforward way to
2 min read
Python Tkinter - Create Button Widget
The Tkinter Button widget is a graphical control element used in Python's Tkinter library to create clickable buttons in a graphical user interface (GUI). It provides a way for users to trigger actions or events when clicked. Note: For more reference, you can read our article: What is WidgetsPython Tkinter OverviewPython Tkinter TutorialTkinter But
4 min read
Change color of button in Python - Tkinter
In this article, we are going to write a Python script to change the color of the button in Tkinter. It can be done with two methods: Using bg properties.Using activebackground properties.Prerequisite: Creating a button in tkinter, Python GUI – Tkinter Change the color of the button in Python - TkinterExample 1: using bg properties. If you want to
2 min read
Tkinter - Button that changes its properties on hover
Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter is the fastest and easiest way to create GUI applications. Creating a GUI using tkinter is an easy task.
2 min read
How To Dynamically Resize Button Text in Tkinter?
Prerequisite: Python GUI – tkinter, Dynamically Resize Buttons When Resizing a Window using Tkinter In this article, we will see how to make the button text size dynamic. Dynamic means whenever button size will change, the button text size will also change. In Tkinter there is no in-built function, that will change the button text size dynamically.
3 min read
Practice Tags :
three90RightbarBannerImg