Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
How To Use Images as Backgrounds in Tkinter?
Next article icon

In this article, we will learn how to load images from user system to Tkinter window using PIL module. This program will open a dialogue box to select the required file from any directory and display it in the tkinter window.
Install the requirements - 
Use this command to install Tkinter : 

pip install python-tk


Use this command to install PIL : 

pip install pillow


Importing modules - 

Python3
fromtkinterimport*# loading Python Imaging LibraryfromPILimportImageTk,Image# To get the dialog box to open when requiredfromtkinterimportfiledialog

Note: The ImageTk module contains support to create and modify Tkinter BitmapImage and PhotoImage objects from PIL images and filedialog is used for the dialog box to appear when you are opening file from anywhere in your system or saving your file in a particular position or place. 
  
Function to create a Tkinter window consisting of a button - 
 

Python3
# Create a windowroot=Tk()# Set Title as Image Loaderroot.title("Image Loader")# Set the resolution of windowroot.geometry("550x300 + 300 + 150")# Allow Window to be resizableroot.resizable(width=True,height=True)# Create a button and place it into the window using grid layoutbtn=Button(root,text='open image',command=open_img).grid(row=1,columnspan=4)root.mainloop()

The Button object is created with text 'open image'. On clicking it the open_image function will be invoked. 
Function to place the image onto the window - 
 

Python3
defopen_img():# Select the Imagename  from a folderx=openfilename()# opens the imageimg=Image.open(x)# resize the image and apply a high-quality down sampling filterimg=img.resize((250,250),Image.LANCZOS)# PhotoImage class is used to add image to widgets, icons etcimg=ImageTk.PhotoImage(img)# create a labelpanel=Label(root,image=img)# set the image as imgpanel.image=imgpanel.grid(row=2)

The openfilename function will return the file name of image. 
Function to return the file name chosen from a dialog box - 
 

Python3
defopenfilename():# open file dialog box to select image# The dialogue box has a title "Open"filename=filedialog.askopenfilename(title='"pen')returnfilename

To run this code, save it by the extension .py and then open cmd (command prompt) and move to the location of the file saved and then write the following - 
 

python "filename".py


and press enter and it will run. Or can be run directly by simply double-clicking your .py extension file. 
  
Output: 
https://media.geeksforgeeks.org/wp-content/uploads/20191121233525/Screencast-from-Thursday-21-November-2019-111137-IST2.webm
 


Improve

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp