Image not loading in tkinter - python

I am attempting to display an image from a file in my program. The python file is in the same folder as my image file and I have the file name correct. When it displays on the screen, the space is taken up where the image should be, but no image shows.
from tkinter import *
from PIL import ImageTk,Image
import tkinter as tk
cardImageLabel=Label(playerFrame, image=tk.PhotoImage(file="2C.png")).grid(row=1, column=0)
This is my code, it's a bit ugly and I should separate it but I'm more used to formatting it like this.

You need to create a reference to the image, to prevent collecting it by garbage collector, as there is no reference created to this object by PhotoImage class itself.
img = tk.PhotoImage(file="2C.png")

Related

Image not showing in tkinter button [duplicate]

This question already has answers here:
Why does Tkinter image not show up if created in a function?
(5 answers)
Closed 3 months ago.
I created a button with an image in Tkinter, but the image isn't showing, i can just see a blank space in the button. Maybe my OS is the problem. I have Linux Mint 21 Xfce Edition.
from tkinter import ttk
from tkinter import filedialog
from tkinter import messagebox
from tkinter import dialog
from tkinter import *
from PIL import ImageTk, Image
from modules import fonts, buttons
import shutil
import os
virusscan_win = Tk()
virusscan_win.title("Virus scan")
virusscan_win.geometry('400x200+60+60')
virusscan_win.configure(background="white")
Message(text="The virus scanner gives them the ability to scan files, folders or their system for viruses and remove threats automatically.", width=250, font=fonts.sansserif_small, background = "white").place(x=10, y=10)
safe_image=PhotoImage(file='./media/button.png')
Button(master=virusscan_win, image=safe_image, text="Scan file").place(x=10, y=90)
Can anyone explain, what was wrong with my code? I already checked, is the file in the correct folder. the debugger also shows nothing.
Python is notorious for aggressively garbage collecting image references, which leads to them being destroyed before the UI gets drawn. Something like this should work for getting an image onto a Button
from PIL import Image, ImageTk
from pathlib import Path
img_path = Path('C:/<path>/<to>/media/button.png') # use the full path to your img file
with Image.open(img_path) as img: # open the image file
img_tk = ImageTk.PhotoImage(img) # get the image as a Tk object
button = Button(virusscan_win, image=img_tk) # add image to button
Note that if your image is the wrong size for your button, you can resize it like so
img_tk = ImageTk.PhotoImage(img.resize(32, 32)) # or whatever size you need

Photoimage is not working in tkinter. Gives an error now but from start

from tkinter import *
from tkinter import ttk
win = Tk()
colorful=PhotoImage(file='image/1.png')
_tkinter.TclError: couldn't recognize data in image file "image/1.png" file path is correct, from beginning it was running without an error but when I add some more code it happened, why?
Try this:
from PIL import Image, ImageTk
img = Image.open("file_name")
colorful = ImageTk.PhotoImage(img)
Tkinter Photoimage only supports .gif images.
If you want to display .png images, Use PIL (Python Imaging Library)
Fore more info, see https://pythonbasics.org/tkinter-image/ .

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

I've spent some time looking online and so far haven't found anything that answers this without using PIL, which i can't get to work is there another way to do this simply?
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?
You can use PIL library which available for python 3.
First, you need to install PIL using this command (using pip):
pip install pillow
then:
`from tkinter import *`
from PIL import Image, ImageTk
`class Window(Frame):`
`def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.pack(fill=BOTH, expand=1)
load = Image.open("parrot.jpg")
render = ImageTk.PhotoImage(load)
img = Label(self, image=render)
img.image = render
img.place(x=0, y=0)`
Luke, this is too late , however may help others. Your image has to be in the same sub directory with .py script. You can also type './imageFileName.png'

How can I display an image using Pillow?

I want to display a gif image using Pillow
Here is my simple code:
from tkinter import *
from PIL import Image, ImageTk
import tkinter as Tk
image = Image.open("Puissance4.gif")
image.show()
But nothing happens...
All help will be appreciated
Thanks!
PIL provides a show method which attempts to detect your OS and choose an
appropriate viewer. On Unix it tries calling the imagemagick command display
or xv. On Macs it uses open, on Windows it uses... something else.
If it can't find an appropriate viewer, ImageShow._viewers will be an empty list.
On Raspbian, you'll need to install an image viewer such as display, xv or fim. (Note a search on the web will show that there are many image viewers available.) Then
you can tell PIL to use it by specifying the command parameter:
image.show(command='fim')
To display an image in Tkinter, you could use something like:
from PIL import Image, ImageTk
import tkinter as tk
root = tk.Tk()
img = Image.open("image.gif")
tkimage = ImageTk.PhotoImage(img)
tk.Label(root, image=tkimage).pack()
root.mainloop()

Python Insert Image to Tkinter Text Widget

Im creating a simple chat box in Python and I want to insert an image(emoticons) to a TKinter text widget. I have tried it using this code:
img = Image.open("icon.jpg")
self.bigText.insert(END, img) # bigText is the text widget
Output of the code above is
<PIL.JpegImagePlugin.JpegImageFile instance at 0x01AB5A30>
instead of the image.
I'm not 100% sure on this, but I think you need to use image_create. Something like:
self.bigText.image_create(END, image=img)
should do the trick.
I have made it using:
from Tkinter import *
from PIL import Image, ImageTk
self.myEmoticons.append(self.smiley)
self.bigText.image_create(END,image = self.myEmoticons[self.myEmoticonsCtr])
self.myEmoticonsCtr=self.myEmoticonsCtr + 1

Categories

Resources