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:
from tkinter import *
from tkinter.ttk import *
root = Tk()
Label(root, text = 'GeeksforGeeks', font =(
'Verdana', 15)).pack(side = TOP, pady = 10)
photo = PhotoImage(file = r"C:\Gfg\circle.png")
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.
from tkinter import *
from tkinter.ttk import *
root = Tk()
Label(root, text = 'GeeksforGeeks', font =(
'Verdana', 15)).pack(side = TOP, pady = 10)
photo = PhotoImage(file = r"C:\Gfg\circle.png")
photoimage = photo.subsample(3, 3)
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.
Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.Dive into the future of technology - explore the
Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
25 Apr, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...