- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
How to add an image in Tkinter?
Images are very useful objects in any application. We can process Images in a Tkinter application using the Pillow or PIL package in Python. There are several built-in functions such as loading an image, extracting an image, configuring the image pane, etc.
In this example, we will add by asking the user to select an image from a dialog box and then, display it using the Label widget.
#Import the Tkinter library from tkinter import * from tkinter import ttk from tkinter import filedialog from PIL import Image, ImageTk #Create an instance of Tkinter frame win= Tk() #Define the geometry win.geometry("750x350") win.title("Image Gallery") def select_file(): path= filedialog.askopenfilename(title="Select an Image", filetype=(('image files','*.jpg'),('all files','*.*'))) img= Image.open(path) img=ImageTk.PhotoImage(img) label= Label(win, image= img) label.image= img label.pack() #Create a label and a Button to Open the dialog Label(win, text="Click the Button below to select an Image", font=('Caveat 15 bold')).pack(pady=20) button= ttk.Button(win, text="Select to Open", command= select_file) button.pack(ipadx=5, pady=15) win.mainloop()
Running the above code will display a window that contains a button to select the image file from the directory and display the image on the window.
Now, select any image from the local directory and display the output on the screen.
- Related Articles
- How to resize an image using Tkinter?
- How to add placeholder to an Entry in tkinter?
- How to center an image in canvas Python Tkinter
- How to update an image in a Tkinter Canvas?
- Add image on a Python Tkinter button
- How to use an Image as a button in Tkinter?
- How to insert an image in a Tkinter canvas item?
- How to use an image for the background in tkinter?
- How to place an image into a frame in Tkinter?
- How to add image smoothing for an Image using FabricJS?
- How to Move an Image in Tkinter canvas with Arrow Keys?
- How to add scroll bar to an image in JavaFX?
- How to add context menu to an image in JavaFX?
- OpenCV Python – How to add borders to an image?
- How to add an image to a button (action) in JavaFX?