Python Forum
Thumbnail picture won't show, and no error. why?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Thumbnail picture won't show, and no error. why?
#1
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.
Larz60+ write Mar-20-2023, 07:05 PM:
As encouraged by deanhystad:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
I have added them for you this time. Please use BBCode tags on future posts.
Reply
#2
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 Python
The 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 labelthumbnail.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.
Ragnar_Itachi likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] the picture disappears berckut72 1 3,517 Mar-24-2020, 08:37 PM
Last Post: berckut72
  a neat picture based on recursive number phillip1882 1 2,817 Sep-06-2018, 02:48 AM
Last Post: scidam
  tkinter picture importation problema SheeppOSU 1 3,001 May-16-2018, 09:26 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020