-2

For learning more about Python and tkinter I am writing a YouTube downloader. I have a separate window where you can add a link and press a button which should display the thumbnail and title inside my main window. Right now I just got the title working.

Here is the function:

def display_yt_video(self, title):

    thumbnail = tk.PhotoImage(file='thumbnail.gif')

    photo_label = tk.Label(root, image=thumbnail)
    photo_label.pack()

    yt_label = tk.Label(gui.bottomFrame, text=title)
    yt_label.pack()

It seems like he can't find the right "root" object, no error is showing. I tried to use the window "root" and also in multiple frames. I managed to open this picture in another program in exactly this way.

Here is my striped down Code:

from pytube import YouTube
import tkinter as tk
import urllib.request

root = tk.Tk()

class Gui:

    def add_url_window(self):

        ck_button = tk.Button(url_topframe, text='Add to Queue')
        ck_button.bind('<Button-1>', dl.check_url)
        ck_button.grid(row=2, column=2, padx=10)


class Downloader():

    def check_url(self, event):
        #try:
        url = self.retrieve_input()
        thumbnail_url = YouTube(url).thumbnail_url
        urllib.request.urlretrieve(thumbnail_url, "thumbnail.gif")
        yt_title = YouTube(url).title
        print('done')
        dl.display_yt_video(yt_title)



    def display_yt_video(self, title):

        thumbnail = tk.PhotoImage(file='thumbnail.gif')

        photo_label = tk.Label(root, image=thumbnail)
        photo_label.pack()

        yt_label = tk.Label(gui.bottomFrame, text=title)
        yt_label.pack()



dl = Downloader(root)
gui = Gui()
root = tk.Tk()
menu = tk.Menu()


root.config(menu=menu)
gui.build_window()


root.mainloop(
klutt
  • 30,332
  • 17
  • 55
  • 95
  • 2
    your code as posted cannot possibly work, you've created two different root windows, you miss a brace after `root.mainloop` you pass `root` to the `Downloader` class although it has no constructor so cannot accept any arguments you call functions you haven't defined (`gui.build_window`) you need to provide a better example before anyone can help you. – James Kent Aug 01 '18 at 12:58

1 Answers1

0

Your code does not run. Besides that I think your problem might be described here: Why does Tkinter image not show up if created in a function?

figbeam
  • 7,001
  • 2
  • 12
  • 18