Python | Add image on a Tkinter button

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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()

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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()

chevron_right


Output:

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.