Reputation: 49
I'm doing a YouTube converter. It's ok but when i download and convert i want to let user know that i'm working: how can i update my interface? I'm using kivy. I tried using a popup, i tried using various kind of loading bar. Now i'm trying to update text in a box in the main interface but nothing, it updates after conversion. Why? Here's my code:
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.popup import Popup
from pydub import AudioSegment
from pytube import YouTube
import os
import time
class YouTube2mp3App(App):
def build(self):
self.mylayout = FloatLayout()
mylabel = Label(text= "Benvenuto su YouTube2mp3!\nInserisci il link e premi converti", size_hint=(.8,.2),pos_hint={"x":.1, "y":.7}, font_size=20)
self.txt = TextInput(hint_text="Incolla qui",size_hint=(.9,.1), pos_hint={"x":.05, "y":.4})
self.pb = Label(text="0%", size_hint=(.9,.2), pos_hint={"x":.1, "y":.1}, font_size=20)
#self.txt.bind(on_text_validate=self.convert)
mybutton =Button(text="Nuovo/i video", on_press= self.clearText,size_hint=(.45,.1), pos_hint={"x":.5, "y":.3})
mybutton2 =Button(text="Converti", on_press= self.convert,size_hint=(.45,.1), pos_hint={"x":.05, "y":.3})
self.mylayout.add_widget(mylabel)
self.mylayout.add_widget(self.txt)
self.mylayout.add_widget(self.pb)
self.mylayout.add_widget(mybutton)
self.mylayout.add_widget(mybutton2)
return self.mylayout
def clearText(self, instance):
#clear input
self.txt.text = ''
def convert(self, instance):
try:
#starting to convert using 3 methods
self.pb.text="Preparazione... 10%"
self.mylayout.add_widget(self.pb)
ns=self.check_spazio(self.txt.text)
self.pb.text="Conversione... 33%"
self.mylayout.add_widget(self.pb)
print(ns)
links=self.list_of_links(ns)
self.pb.text="Scaricamento... 80%"
self.mylayout.add_widget(self.pb)
print(links)
self.Tube(links)
self.pb.text="Fatto!... 100%"
self.mylayout.add_widget(self.pb)
content = Button(text='Chiudi', size_hint=(.3,.7))
popup = Popup(title="Fatto!", content=content, auto_dismiss=False, size_hint=(.3,.2))
content.bind(on_press=popup.dismiss)
popup.open()
self.pb.value="0%"
except:
content = Button(text='Chiudi', size_hint=(.3,.7))
popup = Popup(title="Link non valido", content=content, auto_dismiss=False, size_hint=(.3,.2))
content.bind(on_press=popup.dismiss)
popup.open()
def Tube(self, lista):
#convert
for text in lista:
yt=YouTube(text)
ys=yt.streams.get_audio_only()
file=ys.download()
ya=file[:-1]+"3"
AudioSegment.from_file(file).export(ya, format="mp3")
os.remove(file)
def check_spazio(self, string):
#check space between input string
s=list(string)
strig=[]
for i in s:
if i!=" ":
strig.append(i)
string = "".join(strig)
return string
def list_of_links(self, string):
#create a list of strings. Each string is a link
prev=0
links=[]
for i in range (len(string)):
if string[i]=="\n" or string[i]==",":
links.append(string[prev:i])
prev=i+1
elif i==len(string)-1:
links.append(string[prev:i+1])
return links
YouTube2mp3App().run()
Upvotes: 1
Views: 246
Reputation: 38822
When you run a method on the main thread, as you are doing with convert()
method, no updates are made to the GUI until that method returns. This is a common problem and is typically handled by running the long running method in a separate thread. See the threading documentation. Then, from inside that method use Clock.schedule_once() to schedule calls that update the GUI on the main thread.
Upvotes: 1