![]() |
Thumbnail picture won't show, and no error. why? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: Thumbnail picture won't show, and no error. why? (/thread-39639.html) |
Thumbnail picture won't show, and no error. why? - Ragnar_Itachi - Mar-20-2023 Thumbnail picture won't show, and also no error. why? this is my full code. from tkinter import * from PIL import Image, ImageTk import pytube as py import requests from io import BytesIO root = Tk() root.title('Youtube downloader') root.geometry('700x500') Label(root, text='Welcome to Rag-Chi',font=('Times New Roman',40)).place(anchor=CENTER, x= 350,y=50) Label(root, text='Link: ',font=('Times',21)).place(x=260, y=147, anchor=E) def show_info(): video_url = link.get() youtube = py.YouTube(video_url) video_title = youtube.title video_thumbnail_url = youtube.thumbnail_url response = requests.get(video_thumbnail_url) img_data = response.content img = Image.open(BytesIO(img_data)) resized = img.resize((50,50),Image.LANCZOS) photo = ImageTk.PhotoImage(resized) Title.config(text=video_title) thumbnail.config(image=photo) link = StringVar() url_entry = Entry(root, textvariable=link).place(x=350,y=150, anchor=CENTER) Title = Label(root) Title.place(x= 200,y=400) thumbnail = Label(root,) thumbnail.place(x=700,y=700, anchor=NW) Button(root, text='Get Info',command=show_info).place(x=300,y=200, anchor=CENTER) Button(root, text='Download').place(x=400,y=200, anchor=CENTER) root.mainloop()it displays the title. but thumbnail is not showing. RE: Thumbnail picture won't show, and no error. why? - deanhystad - Mar-20-2023 Please use Python tags when posting code. The image is deleted because it is not referenced anywhere. An easy fix is save a reference to the image in the thumbnail. photo = ImageTk.PhotoImage(resized) Title.config(text=video_title) thumbnail.config(image=photo) thumbnail.photo = photo # Now the image has a reference in PythonThe reason this happens is the way the tkinter package is written. tkinter is a thin wrapper around the Tk library. Most tkinter objects only have one attribute, a handle to a Tk library object. When you assign an Image object to the label thumbnail.config(image=photo) , this passes the Image directly to the Tk library. No reference to the object is saved in tkinter. When you reassign the "photo" variable to the next Image, the reference count for the current Image drops to zero, and Python garbage collects the current image.
|