EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Tkinter Tutorial Tkinter PhotoImage
Secondary Sidebar
Tkinter Tutorial
  • Tkinter
    • Python Tkinter
    • Tkinter Widgets
    • Tkinter background image
    • Tkinter button color
    • Tkinter place
    • Python Tkinter Button
    • Python Tkinter Canvas
    • Tkinter Frame
    • Tkinter LabelFrame
    • Python Tkinter Label
    • Tkinter Text
    • Tkinter Scrollbar
    • Tkinter Listbox
    • Tkinter Spinbox
    • Tkinter Checkbutton
    • Tkinter Menu
    • Tkinter Menubutton
    • Tkinter OptionMenu
    • Tkinter Messagebox
    • Tkinter Grid
    • Tkinter Pack
    • Tkinter Scale
    • Tkinter Table
    • Python Tkinter Entry
    • Tkinter after
    • Tkinter Colors
    • Tkinter Font
    • Tkinter PhotoImage
    • Tkinter TreeView
    • Tkinter Notebook
    • Tkinter Combobox
    • Tkinter Bind
    • Tkinter Icon
    • Tkinter Window Size
    • Tkinter Color Chart
    • Tkinter Slider
    • Tkinter Calculator
    • Tkinter geometry
    • Tkinter image
    • Tkinter input box
    • Tkinter mainloop

Tkinter PhotoImage

By Sivaraman VSivaraman V

Tkinter PhotoImage

Introduction to Tkinter PhotoImage

Tkinter Photoimage is one of the built-in methods which has been used to add the user-defined images in the application. It’s also the keyword that has typically called the parameters file and it is stored in the path we can use the data parameter type. It also must be used for the byte data type object. It contains the image data the photoimage method is used the image has the argument and this argument is used wherever it is needed for the other widgets like the label but with the help, if its instance they can be called and utilized in other widgets.

Syntax:

The Tkinter most powerful GUI based widget it has the most advanced library methods in the python programming language. It has n number of methods it has been used across globally it includes images and animations hence photoimage() is one of the default methods and it can be used directly in the specific requirement widget and these functions will call wherever it requires of the other widget areas. 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 more secure and powerful.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

How does Photoimage Work in Tkinter?

The Tkinter package has a set of built-in functions that has been used for covering the UI parts in both desktop and web application. It has more interactive and advanced library scripts it can be calculated for the time, space functionalities in real-time world, and even though some AI-based application scenarios this will be covered across the widget methods. Basically, we want to decorate the UI has textbox, labels, buttons, views, scroll box, etc mostly button will be the UI area to navigate the web pages. Once we clicked the button it performs the application logic and it works in the backend areas. We can called the photoimage method using file path and the path will be any of the local machine URL paths. And these photoimage class is already used for the displayed images it will be either of the grayscale method or some other true-color images which has been the base64-encoded format of the gif, jpg, and some other image file formats. We can also use this type of embedded code are used in the python codes wherever it requires in the application widget areas. We used a different set of methods in the code along with inside of the UI-related functionalities like iframe and it will covered the entire window frame. Each frame have 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) these 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 widget like Label these images are occupied the areas in the application window.

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,818 ratings)

Constructor

The Tkinter geometry() constructor have default set of arguments that will passed into throughout the programming. Rows and Columns has set of values and these arguments should 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 for to create and passed the arguments in the file image and these PhotoImage constructor which helps to create the object and it needs to calls the other default methods it needs to be more presentation and customized the images.

Methods

We already discussed in the previous paragraphs regarding the photoimage() method and its default behavior and the attributes. The photoimage function will get the method using the PIL packages and these packages will be more user-friendly when compared to the other packages. And these package manager will always use some default options like fill, side, expand, etc. These options will used to controls the photo images in the window screen.

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 have default pattern class that will be read all type of image format files. And these files will be 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 which has already defined in the python library.

Recommended Articles

This is a guide to Tkinter PhotoImage. Here we discuss the introduction of Tkinter PhotoImage, 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
Popular Course in this category
Tkinter Course (1 Course, 1 Project)
  1 Online Courses |  1 Hands-on Projects |  6+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • 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

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

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

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

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

EDUCBA Login

Forgot Password?

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

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

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

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

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

Let’s Get Started

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