Creating an Image (PhotoImage) in Python Tkinter

Posted by

Python Tkinter Add Image

Adding an Image in Tkinter

With Python Tkinter, you can easily add an image to your GUI application using the PhotoImage class. This allows you to display images within your Tkinter window and enhance the visual appeal of your application.

First, you need to import the Tkinter module by using the following line of code:


import tkinter as tk

Next, you can create a new instance of the Tkinter class and specify the window size and title:


root = tk.Tk()
root.title("Image Example")
root.geometry("400x300")

Now, you can load the image file using the PhotoImage class and store it in a variable:


image = tk.PhotoImage(file="example.png")

After loading the image, you can create a label widget and set the image as its background:


label = tk.Label(root, image=image)
label.pack()

Finally, you can use the mainloop() method to run the Tkinter event loop and display the window with the added image:


root.mainloop()

By following these simple steps, you can easily add an image to your Tkinter application and enhance its visual appeal.

0 0 votes
Article Rating
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@zevs8173
10 months ago

Thankyou so much🥰

@lyder4685
10 months ago

How convert png image