Cannot Display an Image in Tkinter
Ok, I figured It out. Apparently due to the way that python deals with garbage disposal the pictures just get erased. A reference to the image in the global scope is required. This is the working code I eventually ended up using:
self.photo = PhotoImage(file="noart.ppm")
self.Artwork = Label(self.frame, image=self.photo)
self.Artwork.photo = self.photo
self.Artwork.pack()
that self.Artwork.photo = self.photo
is the important part. It ensures that the Image will be shown.
Tkinter can't display image on button
You aren't creating the image properly. The first argument is a name for the image, not the file. The filename needs to be assigned to the file
option.
Change this:
self.flag_image = PhotoImage("Flag.png", width=30, height=30)
To this:
self.flag_image = PhotoImage(file="Flag.png", width=30, height=30)
Why does Tkinter image not show up if created in a function?
The variable photo
is a local variable which gets garbage collected after the class is instantiated. Save a reference to the photo, for example:
self.photo = tkinter.PhotoImage(...)
If you do a Google search on "tkinter image doesn't display", the first result is this:
Why do my Tkinter images not appear? (The FAQ answer is currently not outdated)
Why can't I display an Image in a tkinter Toplevel() window?
Your playsound() command is blocking execution. The playsound() command has an optional field 'block', which is True by default. Changing this to False will continue execution and allow mainloop() to continue.
Second, just call label.draw() to draw your image to the TopLevel window.
Here's the code:
from tkinter import *
from PIL import Image, ImageTk
from playsound import playsound
def play():
window = Toplevel()
window.attributes('-fullscreen', True)
img = ImageTk.PhotoImage(Image.open("pic.jpeg"))
label = Label(window, image=img).pack()
playsound("song.mp3",block=False)
label.draw()
buttonWindow = Tk()
b = Button(buttonWindow, text="Press Button", command=play)
b.pack()
buttonWindow.mainloop()
Cheers!
Getting user to select and image and display that said image in Tkinter
I figured it out if anyone else gets stuck. Simply make sure that img is in scope throughout the runtime of the program. What I did was set self.image = img
rather than the filename, and then passed self.image
into tk.Label()
as a parameter.
not able to open image in tkinter window, task was to make a gallery of images using os module but some error occurs as nothing is displayed on Window
Don't use generic from tkinter import *
, but rather specify explicitly what you are using.
For image display, this code should work:
image = ImageTk.PhotoImage(Image.open(filepath))
label = Label(image=image)
label.image = image
label.pack()
with filepath = os.path.join(root, file)
for full path instead of file
which is only filename.
Moreover, you make a lot of mistakes... Have you tried your code ?
For instance, endwith
don't exist. Use endswith
instead. And is your path really "/img"
? Shouldn't it be "./img"
?
Related Topics
Django-Registration & Django-Profile, Using Your Own Custom Form
Find Usa Phone Numbers in Python Script
Use Aws Glue Python with Numpy and Pandas Python Packages
How to Change the Datetime Tick Label Frequency for Matplotlib Plots
How to Call Python Code from C Code
Preprocessing in Scikit Learn - Single Sample - Depreciation Warning
Type Hints with User Defined Classes
Does Flask Support Regular Expressions in Its Url Routing
How to Merge Two Lists into a Single List
How to Isolate Everything Inside of a Contour, Scale It, and Test the Similarity to an Image
Split an Integer into Digits to Compute an Isbn Checksum
How to Create an SQL View with SQLalchemy
Iterate Over All Combinations of Values in Multiple Lists in Python