- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- How to resize an image using Tkinter?
- Add image on a Python Tkinter button
- How to update an image in a Tkinter Canvas?
- How to center an image in canvas Python Tkinter
- How to add placeholder to an Entry in tkinter?
- How to use an image for the background in tkinter?
- How to use an Image as a button in Tkinter?
- How to insert an image in a Tkinter canvas item?
- How to place an image into a frame in Tkinter?
- How to add scroll bar to an image in JavaFX?
- How to add context menu to an image in JavaFX?
- How to add an image as label using JavaFX?
- How to Move an Image in Tkinter canvas with Arrow Keys?
- How to add a border to an image with CSS?
- How to add a button to an image with CSS?