Home > Enterprise >  Updating a customtkinter Label after input is placed into an Entry field
Updating a customtkinter Label after input is placed into an Entry field

Time:12-11

Programming a Private YouTube video downloader and I want the title and views on the UI to update when I put a new YouTube link into the field. Code:

import customtkinter as tk
from script import *
from pytube import YouTube



root = tk.CTk()
root.geometry('690x700')
root.title('YouTube to Video Converter')
tk.set_appearance_mode('System')
tk.set_default_color_theme('YT_Downloader/theme.json')
red = '#E70000'
red_selected ='#AB0000'
yt = YouTube('https://www.youtube.com/watch?v=7yn-0-1GIrk')
frame = tk.CTkFrame(master=root,corner_radius=10)
frame.grid(row=0,column=0, padx=20, pady=20)

title = tk.CTkLabel(frame, text = 'YouTube to Video Converter', font =('Roboto', 50))
title.grid(row=1, column=0,columnspan=3, pady=10, padx = 12, sticky='nsew')
url = tk.CTkEntry(frame, placeholder_text='https://www.youtube.com/watch?v=7yn-0-1GIrk', font=('Roboto', 12),width=250, height=20)
url.grid(row=3, column=2, columnspan=2, padx=10, pady=12)

video_name = tk.CTkLabel(frame, text=(f'Title: {yt.title}'), font=('Roboto', 18))
video_name.grid(row=2, column=0, columnspan=2, padx=5)

video_views = tk.CTkLabel(frame, text=(f'Views: {yt.views}'), font=('Roboto', 18))
video_views.grid(row=3, column=0, columnspan=2, padx=5)
while 1==1:
    if len(url.get()) == 0:
        yt = YouTube('https://www.youtube.com/watch?v=7yn-0-1GIrk')
    else:
        yt = YouTube(url.get().strip())
        root.update_idletasks()
        #video_name.config(text=(f'Title: {yt.title}'))
        #video_views.config(text=(f'Views: {yt.views}'))

def egt():
    print(url.get())
    print(len(url.get()))


enter_url = tk.CTkLabel(frame, text='Please Insert your URL in the Box Below', font=('Roboto', 18))
enter_url.grid(row=2, column=2, columnspan=2, padx=5)


button = tk.CTkButton(frame, text='Convert', font=('roboto', 18), command=egt )
button.grid(row=5, column=2)
root.mainloop()

Script.py:

from pytube import *

yt=''

def Process():
    try:
        stream = yt.streams.get_highest_resolution()
        stream.download('YT_Downloader/videos')
    except Exception:
        stream = yt.streams.get_highest_resolution()
        stream.download('YT_Downloader/videos')

I'm not sure what else to do other than putting it into an infinite while loop like shown above which doesn't launch the program at all.

CodePudding user response:

You can attach a callback to a stringvariable, which is called everytime the variable is updated. In your case you could do something like:

def callback(str_var):
    # Set your label here. The string is accessible through str_var.get()
    print(str_var.get())

url_var=StringVar()

url = tk.CTkEntry(frame, placeholder_text='https://www.youtube.com/watch?v=7yn-0-1GIrk', font=('Roboto', 12),width=250, height=20
textvariable=url_var)

url_var.trace("w", lambda name, index, mode, url_var=url_var: callback(url_var))
  • Related