My Tkinter photo is packed, but it shows as a blank - python

I can't seem to get the picture to show up on screen while having a toplevel frame on top of my main one (root). This one is just called "frame". I have circled the outline of the tkinter Frame on the included photo in this post. When I resize the picture, the green frame outline changes, but the picture itself won't show up.
I also tried to pack it on my main root window, with success, which suggests it's a toplevel window issue. I just don't know what it is. Any ideas?
Here is my code:
def show_topLevelWindow():
from tkinter import ttk
print("Entered to show results")
window_linkedin = Toplevel(root)
window_linkedin.geometry('1000x590')
frame = Frame(window_linkedin)
frame.pack()
error_frame = tkinter.Frame(frame, highlightbackground="green", highlightcolor="green", highlightthickness=1)
error_label = Label(frame, text="It appears there are no results for the selected country")
error_label.config(font=("Helvetica Neue", 20))
im_error = Image.open("./ressources/images_gui/aw_snap.png")
im_error = im_error.resize((500, 500), Image.ANTIALIAS)
im_error = ImageTk.PhotoImage(file = "./ressources/images_gui/aw_snap.png")
im_error_label = Label(frame, image=im_error)
try:
if ....:
....unimportant code ....
else:
error_label.pack(in_=error_frame)
im_error_label.pack(in_=error_frame)
error_frame.pack(anchor="center")
except Exception as e:
error_label.pack(in_=error_frame)
im_error_label.pack(in_=error_frame)
error_frame.pack(anchor="center")
Packed image shows as blank

The single most important issue you are having is your image is not being saved for reference. if you add global im_error to the very top of your function your image will be visible.
That said there are some issues with your code you should correct.
First: Do not import in a function. Instead write all your imports at the top of your code.
Second: I am not sure why you are doing .pack(in_=error_frame). This is not something one would ever really need. Just make sure that your label is already assigned to the correct frame. The in_ argument is rarely used and probably most people never use it. I have been on here for two years now and this is the first time I have seen anyone use that argument.
Third: You have not shown your imports for Tkinter however based on how you have written you code it looks like you have done:
import tkinter
from tkinter import *
This is overkill and is not a good idea. Just do import tkinter as tk and make sure you use tk. prefix where it applies.
Here is your code remade:
import tkinter.ttk as ttk
import tkinter as tk
from PIL import ImageTk, Image
def show_toplevel_window():
global im_error
window_linkedin = tk.Toplevel(root)
window_linkedin.geometry('1000x590')
frame = tk.Frame(window_linkedin)
frame.pack()
error_frame = tk.Frame(frame, highlightbackground="green", highlightcolor="green", highlightthickness=1)
error_frame.pack()
error_label = tk.Label(frame, font=("Helvetica Neue", 20), text="It appears there are no results for the selected country")
error_label.pack()
im_error = Image.open("./ressources/images_gui/aw_snap.png")
im_error = im_error.resize((500, 500), Image.ANTIALIAS)
im_error = ImageTk.PhotoImage(file = "./ressources/images_gui/aw_snap.png")
im_error_label = tk.Label(error_frame, image=im_error)
im_error_label.pack()
root = tk.Tk()
show_toplevel_window()
root.mainloop()

Related

TkinterVideoPlayer stops resizing the video when it's put inside a frame. How can I stop this?

I'm trying to make a video player that play's a video full screen. The user is also able to toggle onto another frame that I want to take up the full GUI and to be able to toggle in between them.. I have written some code and It works fine when I pack the video player to root but then when I put it inside a frame it shrinks down to a much smaller size than the GUI. Why is this happening and how can I fix it?
This is the code that has the video playing and resizing to the GUI size dynamically
import datetime
import tkinter as tk
from tkinter import *
from turtle import width
from tkVideoPlayer import TkinterVideo
#changing the visible frame on the screen
def showFrame (frame):
frame.tkraise()
root = tk.Tk()
root.title("Hology")
width1, height1= int(root.winfo_screenwidth()), int(root.winfo_screenheight())
root.geometry(str(width1-200) + "x" + str(height1-200))
vid_player = TkinterVideo(master=root, scaled=True)
# vid_player.set_size((width1-200, height1-200))
vid_player.pack(expand=True, fill=BOTH)
progress_value = tk.IntVar(root)
vid_player.bind("<<SecondChanged>>", loopVid)
load_file= "hol.mp4"
vid_player.load(load_file)
vid_player.play()
# vid_player.pack(expand=True, fill="both")
holo.tkraise()
root.mainloop()
This is where it is placed inside a frame
import datetime
import tkinter as tk
from tkinter import *
from turtle import width
from tkVideoPlayer import TkinterVideo
global vidVal
vidVal = 0
#changing the visible frame on the screen
def showFrame (frame):
frame.tkraise()
root = tk.Tk()
root.title("Hology")
width1, height1= int(root.winfo_screenwidth()), int(root.winfo_screenheight())
root.geometry(str(width1-200) + "x" + str(height1-200))
holo = Frame(root)
timeline = Frame(root)
holo.grid(row=0,column=0)
timeline.grid(row=0,column=0)
# root.attributes('-fullscreen',True)
# for frame in (holo, timeline):
# frame.master.geometry(str(width1-200) + "x" + str(height1-200))
# # frame.grid(row=1, column=1, columnspan=10, rowspan=8, sticky=tk.EW)
# # frame.pack_propagate(False)
# frame.pack()
vid_player = TkinterVideo(master=holo, scaled=True)
# vid_player.set_size((width1-200, height1-200))
vid_player.pack(expand=True, fill=BOTH)
load_file= "hol.mp4"
vid_player.load(load_file)
vid_player.play()
# vid_player.pack(expand=True, fill="both")
holo.tkraise()
root.mainloop()
Any help enabling me to keep the video at full size when inside a frame would be greatly appreciated. I have spent some time looking through the documentation and I know it will be a simple fix or something silly, I'm just stuck! :(
Thank you

How can my program show image when I click button? [duplicate]

This question already has answers here:
Why does Tkinter image not show up if created in a function?
(5 answers)
Closed 10 months ago.
I was trying to show the image when I clicked the button however it failed( the program does not report an error but the image is still not displayed). I'm sure I put the right path to the picture. This is code
import tkinter as tk
from PIL import ImageTk, Image
window = tk.Tk()
def def_btn1():
image1 = Image.open("csdl.png")
image1 = image1.resize((710, 400), Image.ANTIALIAS)
test = ImageTk.PhotoImage(image1)
lbl2 = tk.Label(image=test)
lbl2.pack()
btn1 = tk.Button(text="click to show", relief=tk.RIDGE, width=15, font=(12),command=def_btn1)
btn1.pack()
window = tk.mainloop()
I want when I click the button the image will show in the program. Thank you!
You need to add lbl2.image = test for the image to not be destroyed by tkinter, you new code should be this -
import tkinter as tk
from PIL import ImageTk, Image
window = tk.Tk() # You might want to move this line below your functions, with the other global variables
def def_btn1():
image1 = Image.open("csdl.png")
image1 = image1.resize((710, 400), Image.ANTIALIAS)
test = ImageTk.PhotoImage(image1)
lbl2 = tk.Label(image=test)
lbl2.image = test # You need to have this line for it to work, otherwise it is getting rid of the image. the varibale after the '=' must be the same as the one calling "ImageTk.PhotoImage"
lbl2.pack()
btn1 = tk.Button(text="click to show", relief=tk.RIDGE, width=15, font=(12),command= lambda : def_btn1()) # adding 'lambda :' stops the function from running straight away, it will only run if the button is pressed
btn1.pack()
window = tk.mainloop()

Changing the color of Tkinter canvas after set period of time

I'm trying to set a Tkinter canvas to red/green for one second, then back to white afterward. However, despite the fact that the code setting the canvas to red/green precedes the code reverting back to white, the window doesn't reflect the initial color change. I understand that by calling .after, the program freezes until the specified duration is over, but I don't understand why it doesn't change to red or green before freezing.
if is_correct:
self.canvas.config(bg="green")
else:
self.canvas.config(bg="red")
self.window.after(1000, self.canvas.config(bg="white"))
Refer to this simple program.
from tkinter import *
root=Tk()
def change_bg():
canvas.config(bg="red")
root.after(1000,lambda: canvas.config(bg="white"))
canvas=Canvas(root,bg="white")
canvas.pack()
root.after(1000,change_bg)
root.mainloop()
from tkinter import *
import time
def change_color():
can.config(bg="red")
can.update()
change_color2()
def change_color2():
time.sleep(1)
can.config(bg="white")
root = Tk()
root.geometry("500x500")
can = Canvas(root, bg="white", height=450, width=500)
can.pack()
Button(root, text="Change color for 1 sec", command=change_color).pack()
root.mainloop()
You can refer to this code

Unable to display image using tkinter [duplicate]

This question already has answers here:
Why does Tkinter image not show up if created in a function?
(5 answers)
Closed 5 years ago.
I have the following code:
from tkinter import *
import os
from PIL import ImageTk, Image
#Python3 version of PIL is Pillow
class Scope:
def __init__(self, master):
self.master = master
f = Frame(root)
self.greet_button = Button(f, text="Back", command=self.back)
self.greet_button.pack(side= LEFT)
self.greet_button = Button(f, text="Detect", command=self.detect)
self.greet_button.pack(side= LEFT)
self.close_button = Button(f, text="Continue", command=master.quit)
self.close_button.pack(side=LEFT)
photo = PhotoImage(file='demo.gif')
cv = Label(master, image=photo)
cv.pack(side= BOTTOM)
f.pack()
def greet(self):
print("Previous image...")
root = Tk()
my_gui = Scope(root)
root.mainloop()
My first problem is that when I run this, all the buttons and the window show up, but there is no image. There is a square place holder indicating that the image should be in that box, but no image actually shows. I'm able to display the image if I just type the following:
root = Tk()
photo = PhotoImage(file='demo.gif')
label = Label(root, image=photo)
label.pack()
root.mainloop()
So, I know it's possible. But I don't know what I'm doing wrong with my GUI code. I've tried debugging this quite a bit and nothing seemed to work.
A second problem is, I am completely unable to display a jpg file in a GUI. I've tried using every tutorial and nothing quite does the trick. Ideally, I'd like to just be able to display a jpg image, if that's not possible, I'll settle for displaying a gif.
Your reference to photo gets destroyed / carbage collected by Python after the class is called, so, there is nothing the label could show.
In order to avoid this, you have to maintain a steady reference to it, i.e. by naming it self.photo:
from tkinter import *
import os
from PIL import ImageTk, Image
#Python3 version of PIL is Pillow
class Scope:
def __init__(self, master):
self.master = master
f = Frame(root)
self.greet_button = Button(f, text="Back") #, command=self.back)
self.greet_button.pack(side=LEFT)
self.greet_button = Button(f, text="Detect") #, command=self.detect)
self.greet_button.pack(side=LEFT)
self.close_button = Button(f, text="Continue", command=master.quit)
self.close_button.pack(side=LEFT)
self.photo = PhotoImage(file='demo.gif')
cv = Label(master, image=self.photo)
cv.pack(side=BOTTOM)
f.pack()
def greet(self):
print("Previous image...")
root = Tk()
my_gui = Scope(root)
root.mainloop()
PS: Your code snippet was not running properly because two functions were missing.

Python Tkinter Label in Frame

I want to place a label inside a frame in tkinter, but I can't figure out how to actually get it inside.
import tkinter
from tkinter import *
W=tkinter.Tk()
W.geometry("800x850+0+0")
W.configure(background="lightblue")
FRAME=Frame(W, width=100, height =50).place(x=700,y=0)
LABEL=Label(FRAME, text="test").pack()
When I run this, it doesn't place the Label inside the frame, but just places it normally on the window.
What am I doing wrong?
In the line
FRAME=Frame(W, width=100, height =50).place(x=700,y=0)
You think you are returning a tk frame, but you are not! You get the return value of the place method, which is None
So try
frame = Frame(W, width=100, height=50)
frame.place(x=700, y=0)
label = Label(frame, text="test").pack()
If you don't want the frame to shrink to fit the label, use (How to stop Tkinter Frame from shrinking to fit its contents?)
frame.pack_propagate(False)
Note: Either import tkinter or from tkinter import * but not both. Also, by convention, names of instances of objects are lowercase.
I think it's because you're assigning FRAME to Frame(W, width=100, height =50).place(x=700,y=0), as opposed to just the actual frame, and according to the Place Manager reference, there doesn't seem to be a return value. Try this:
import tkinter
from tkinter import *
W=tkinter.Tk()
W.geometry("800x850+0+0")
W.configure(background="lightblue")
FRAME=Frame(W, width=100, height =50)
FRAME.place(x=700,y=0)
LABEL=Label(FRAME, text="test").pack()
W.mainloop()

Categories