Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I insert a JPEG image into a python Tkinter window?

How do I insert a JPEG image into a Python 2.7 Tkinter window? What is wrong with the following code? The image is called Aaron.jpg.

#!/usr/bin/python

import Image
import Tkinter
window = Tkinter.Tk()

window.title("Join")
window.geometry("300x300")
window.configure(background='grey')

imageFile = "Aaron.jpg"

window.im1 = Image.open(imageFile)


raw_input()
window.mainloop()
like image 336
Aaron Esau Avatar asked May 28 '14 00:05

Aaron Esau


People also ask

Can you use JPG in tkinter?

Images can be shown with tkinter. Images can be in a variety of formats including jpeg images.

Can you import images into tkinter?

Image can be added with the help of PhotoImage() method. This is a Tkinter method which means you don't have to import any other module in order to use it.

How do I open a JPG file in Python?

open() Opens and identifies the given image file. This is a lazy operation; this function identifies the file, but the file remains open and the actual image data is not read from the file until you try to process the data (or call the load() method).


1 Answers

Try this:

import tkinter as tk
from PIL import ImageTk, Image

#This creates the main window of an application
window = tk.Tk()
window.title("Join")
window.geometry("300x300")
window.configure(background='grey')

path = "Aaron.jpg"

#Creates a Tkinter-compatible photo image, which can be used everywhere Tkinter expects an image object.
img = ImageTk.PhotoImage(Image.open(path))

#The Label widget is a standard Tkinter widget used to display a text or image on the screen.
panel = tk.Label(window, image = img)

#The Pack geometry manager packs widgets in rows or columns.
panel.pack(side = "bottom", fill = "both", expand = "yes")

#Start the GUI
window.mainloop()

Related docs: ImageTk Module, Tkinter Label Widget, Tkinter Pack Geometry Manager

like image 197
NorthCat Avatar answered Oct 12 '22 18:10

NorthCat