How to Insert a Jpeg Image into a Python Tkinter Window

How to add an image in Tkinter?

There is no "Syntax Error" in the code above - it either ocurred in some other line (the above is not all of your code, as there are no imports, neither the declaration of your path variable) or you got some other error type.

The example above worked fine for me, testing on the interactive interpreter.

how do i open a jpg image inside a tkinter window in python 3.6?

I would check which directory the hello.jpg file is in. The error you're getting means that it couldn't find that file where it was looking. It currently is looking in the same folder that the python file is in


Solution

Make sure that the directory which this python script is in also has the image

Your file should look something like this:

You should have some folder that this python file is in, and in that same folder that the python file is in you should have the TKinter images

On repl.it you should press this button:

Picture of repl.it

Then add the hello.jpg file

You also need to change your code to remove the random indents as well as adding root = Tk() to "activate" TkInter

Your new code should look like this:

from tkinter import *

root = Tk()

# pip install pillow
from PIL import Image, ImageTk
load = Image.open("hello.jpg")
render = ImageTk.PhotoImage(load)

img = Label(self, image=render)
img.image = render
img.place(x=0, y=0)

How to load an image into a python 3.4 tkinter window?

tkinter has a PhotoImage class which can be used to display GIF images. If you want other formats (other than the rather outdated PGM/PPM formats) you'll need to use something like PIL or Pillow.

import Tkinter as tk

root = tk.Tk()
image = tk.PhotoImage(file="myfile.gif")
label = tk.Label(image=image)
label.pack()
root.mainloop()

It's important to note that if you move this code into a function, you may have problems. The above code works because it runs in the global scope. With a function the image object may get garbage-collected when the function exits, which will cause the label to appear blank.

There's an easy workaround (save a persistent reference to the image object), but the point of the above code is to show the simplest code possible.

For more information see this web page: Why do my Tkinter images not appear?

How to insert a photo into a Tkinter window no PIL of pillow

You can just use Tkinter.PhotoImage for this. Minimal example:

root = Tkinter.Tk()
image = Tkinter.PhotoImage(file="img.gif")
Tkinter.Label(root, image=image).pack()
root.mainloop()

Be wary, though, that those PhotoImages tend to be garbage-collected even when used in a Label, so you should either define it on global scope, or add it to some global list or dictionary.

Python, tkinter: Why is this jpeg not showing?

This is a wild guess now, but I just remembered a similar problem. I was able to reproduce your "blue box" this way, so this might be your problem, too. I'll just give it a try.

I assume that the PhotoImage is created in some other scope (maybe a method showImage(self, id) or something like that) and no reference to it is kept beyond the scope of this method. This has the effect that the PhotoImage will be garbage-collected at the end of this method, even though it is used in the Label!

Try creating a variable that exists for the whole life time of the frame and bind the PhotoImage to that variable (e.g. self.images[ID] when using a class for the GUI, or some global variable otherwise). If I am right, and this is indeed the problem, then this should help.

Is .png the only format acceptable in python's tkinter module?

The docstring for PhotoImage says:

class PhotoImage(Image):
"""Widget which can display images in PGM, PPM, GIF, PNG format."""

So no, PNG is not the only acceptable format. PGM, PPM, GIF, and PNG are. JPEG is not.



Related Topics



Leave a reply



Submit