EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Software Development Software Development Tutorials Tkinter Tutorial Tkinter PhotoImage

Tkinter PhotoImage

Sivaraman V
Article bySivaraman V
Priya Pedamkar
Reviewed byPriya Pedamkar

Updated June 20, 2023

Tkinter PhotoImage

Introduction to Tkinter PhotoImage

The Tkinter PhotoImage is a built-in method that allows you to add user-defined images to your application. It serves as a keyword that is typically used with the file parameter where the image file is specified. The image file can be stored in the specified path, and you can also use the data parameter to directly embed the image data as a byte data type object. The PhotoImage method creates an image object that can be utilized wherever it is needed, such as in other widgets like labels.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax:

The Tkinter’s most powerful GUI-based widget has the most advanced library methods in the Python programming language. It has its own syntax and default parameters.

from tkinter import * //import all the library of the tkinter module
from PIL import Image, ImageTk
variable name=Tk()
variable name=PhotoImage(file="")
variable name.mainloop()

The above codes are the basic algorithm for creating the geometry function in the tkinter library, and these modules will utilize all other widgets for presenting the desktop application as more secure and powerful.

How does Photoimage Work in Tkinter?

Basically, we want to decorate the UI with has textbox, labels, buttons, views, scroll boxes, etc most buttons will be the UI area to navigate the web pages. Once we click the button, it performs the application logic and works in the backend areas. We can call the photoimage method using a file path, and the path will be any of the local machine URL paths. It will be either the grayscale method or some other true-color images, which have been the base64-encoded format of the gif, jpg, and some other image file formats. We used a different set of methods in the code along with inside of the UI-related functionalities like iframe, and it will cover the entire window frame. Each frame has a different set of patterns for creating a normal and complex layout screen. It forms the group for including along the widgets functions like Extra frames etc.

While we using the photoimage(), the images are read using the Python imaging library(pil). This library will be applicable for loading the images, and it is a more required and available module for the Pypi package manager in the Python language. And these libraries must support the file formats like PPM,PNG,GIF.JPG, JPEG, BMP, etc these formats are included in the library package with the help of UI widgets like Label. These images have occupied the areas in the application window.

Constructor

Rows and Columns have a set of values, and these arguments should be passed along the runtime.

from Tkinter import *
from PIL import Image, ImageTk
class classname(tk.Tk):
def _init_(variable name,first arguments,second arguments)
variable name= ImageTk.PhotoImage(fileurl)

The above codes will be used to create and pass the arguments in the file image, and this PhotoImage constructor helps to create the object. It needs to call the other default methods. It needs to be more presentation and customized images.

Methods

We already discussed in the previous paragraphs the photoimage() method and its default behavior and attributes. And these package managers will always use default options like fill, side, expand, etc.

variable name=PhotoImage(file=””)
variable name.pack(fill=BOTH,expand=2)

Examples of Tkinter Photoimage

Lets us discuss examples of Tkinter Photoimage.

Example #1

Code:

from tkinter import *
from tkinter.ttk import *
first = Tk()
Label(first, text = 'Welcome To My Domain', font =(
'Verdana', 17)).pack(side = LEFT, pady = 11)
imge = PhotoImage(file = r"C:\Users\Kripya-PC\Downloads\MicrosoftTeams-image (11).png")
Button(first, text = 'Have a Nice Day !', image = imge).pack(side = LEFT)
mainloop()

Output:

Tkinter PhotoImage 1-1

Example #2

Code:

from tkinter import *  
import tkinter as tk
from tkinter import ttk
first = Tk()  
first.geometry("132x110")  
frm = Frame(first)  
frm.pack()  
frm1 = Frame(first)  
frm1.pack(side = RIGHT)  
frm2 = Frame(first)  
frm2.pack(side = LEFT)  

butn = Button(frm, text="Welcome", fg="pink",activebackground = "green")  
butn.pack(side = LEFT)  
  
butn1 = Button(frm, text="To", fg="orange", activebackground = "red")  
butn1.pack(side = RIGHT)  
  
butn2 = Button(frm1, text="My", fg="green", activebackground = "violet")  
butn2.pack(side = RIGHT)  
  
butn3 = Button(frm2, text="Domain", fg="blue", activebackground = "yellow")  
butn3.pack(side = LEFT)  
comb = tk.Label(first,
                    text = "Welcome Back",background = 'pink', foreground ="pink",  
          font = ("Times New Roman", 17))
result = Button(first, text = "Have a Nice Day")
example = ttk.Combobox(first, 
                            values=[
                                    "first", 
                                    "second",
                                    "third",
                                    "four"])
imge = PhotoImage(file = r"C:\Users\Kripya-PC\Downloads\MicrosoftTeams-image (11).png") 
Button(first, text = 'Have a Nice Day !', image = imge).pack(side = LEFT)              

first.mainloop()

Output:

Tkinter PhotoImage 1-2

Example #3

Code : 

from tkinter import *  
import tkinter as tk
from tkinter import ttk
first = Tk()  
first.geometry("132x110")  
frm = Frame(first)  
frm.pack()  
frm1 = Frame(first)  
frm1.pack(side = RIGHT)  
frm2 = Frame(first)  
frm2.pack(side = LEFT)   
imge = PhotoImage(file = r"C:\Users\Kripya-PC\Downloads\MicrosoftTeams-image (11).png") 
Button(first, text = 'Have a Nice Day !', image = imge).pack(side = LEFT) 
labels=Label(first, image=imge, width=320, height=350)
labels.pack(side=BOTTOM)

btn1=Button(first, text=" +" )
btn1.pack(side=TOP)

btn2=Button(first, text=" - ")
btn2.pack(side=RIGHT)                                  

first.mainloop()  

Output:

example 3-1

Conclusion

The Photoimage method has a default pattern class that will read all types of image format files. And these files will convert the image into the binary source code, which has the readable format of the machine. The image library will include all the supported classes already defined in the Python library.

Recommended Articles

This is a guide to Tkinter PhotoImage. Here we discuss the introduction of Tkinter PhotoImage, and how to work PhotoImage with sample code for better understanding. You may also have a look at the following articles to learn more –

  1. Tkinter after
  2. Python Tkinter Entry
  3. Tkinter Grid
  4. Tkinter Combobox
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
Financial Analyst Masters Training Program
2000+ Hours of HD Videos
43 Learning Paths
550+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Data Science Bundle
2000+ Hour of HD Videos
80 Learning Paths
400+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Software Development Bundle
5000+ Hours of HD Videos
149 Learning Paths
1050+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
All in One Software Development Bundle5000+ Hours of HD Videos | 149 Learning Paths | 1050+ Courses | Verifiable Certificate of Completion | Lifetime Access
Financial Analyst Masters Training Program2000+ Hours of HD Videos | 43 Learning Paths | 550+ Courses | Verifiable Certificate of Completion | Lifetime Access
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more