I’m trying to use flet to build a simple app for downloading youtube videos, but I’m also learning Python at the same time.
My problem is that I’m buldinh the components in classes and I need my second class to have real-time acess to some data. It loads it in the beggining, but when the values change in the first class it doesn’t update on the second one.
I’ve tried passing the values directly from my “page.add()” but it worked the same way.
Rhis is my code, it’s messy, I know:
from yt_dlp import YoutubeDL
import flet as ft
import func
class ID_Section(ft.Column):
def __init__(self):
super().__init__()
self.horizontal_alignment = ft.CrossAxisAlignment.CENTER
self.valid_id = False
self.txt_video_id = ft.TextField(
label='Cole o ID do vídeo/short/playlist aqui...', on_change=self.make_invalid
)
self.bt_validate_id = ft.ElevatedButton(text="Validar", on_click=self.validate)
self.txt_id_status = ft.Text('Insira uma ID para começar!', size=14, color='yellow')
self.controls = [
ft.Row([
self.txt_video_id,
self.bt_validate_id,
],
ft.MainAxisAlignment.CENTER
),
self.txt_id_status,
]
def check_id(self, e):
if self.valid_id:
self.txt_id_status.value='ID válida!'
self.txt_id_status.color='green'
elif self.txt_video_id.value== '':
self.txt_id_status.value='Insira uma ID para começar!'
self.txt_id_status.color='yellow'
else:
self.txt_id_status.value='Insira uma ID válida!'
self.txt_id_status.color='red'
def make_invalid(self, e):
self.valid_id = False
self.check_id(e)
self.update()
def validate(self, e):
if self.txt_video_id.value != '':
self.valid_id = func.link_validation(self.txt_video_id.value)
self.check_id(e)
self.update()
class Download_Section(ft.Column):
def __init__(self):
super().__init__()
self.horizontal_alignment = ft.CrossAxisAlignment.CENTER
self.valid = ID_Section().valid_id
self.vid_id = ID_Section().txt_video_id.value
self.btn_download = ft.CupertinoFilledButton(
content=ft.Text("Baixar"),
disabled_color='yellow',
on_click=self.download_vid
)
self.controls = [
ft.Row(),
self.btn_download,
]
def download_vid(self, e):
print(self.valid)
print(self.vid_id)
def main(page: ft.Page):
page.title = 'YouTube Downloader'
page.vertical_alignment = ft.MainAxisAlignment.CENTER
page.horizontal_alignment = ft.CrossAxisAlignment.CENTER
valid = ID_Section().valid_id
page.add(
ft.Text('YouTube Downloader', size=28),
ft.Row(height=20),
ID_Section(),
Download_Section()
)
ft.app(main)
The “ID_Section* class takes in a YouTube ID (video, playlist or short) and validates it. What I want is to be able to have that self.valid_id (a boolean) and self.txt_video_id (the id in the textbox) in the second class, Download_Sectio.
It works when the app starts and it prints the values, but once I cahncge them in the app it stays the same as in the beggining.
Jizon Carlos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1