为什么 kivy 不写入 TextArea

Why kivy doesn't write to TextArea

我尝试了不同的解决方案,但没有一个有效。 这是我最后一次尝试。

当脚本第一次加入时尝试在控制台 "porco" 打印但没有写入 text.result 标签,为什么???直接跳到最后一个 Label write for download 完成

import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from pytube import YouTube

class InputLink(GridLayout):
    def __init__(self, **kwargs):
        super(InputLink, self).__init__(**kwargs)
        self.rows = 4

        self.add_widget(Label(text="Link Youtube:"))
        self.link = TextInput(multiline=False)
        self.add_widget(self.link)

        self.result = Label(text="testo")
        self.add_widget(self.result)

        self.bottone1 = Button(text="Download")
        self.bottone1.bind(on_press=self.click1)
        self.add_widget(self.bottone1)

    def click1(self,btn):
        self.result.text = self.link.text
        yt = ""
        #print(yt.streams.filter(only_audio=True).all())
        try:
            yt = YouTube(self.link.text)
            self.result.text = "Avvio il download di "+self.link.text#<--WHY??
            print('porco')
        except Exception as e:
            self.result.text = "Errore 1"+str(e)
            return
        self.download(yt)
    def download(self,yt):
        try:
            yt.streams.filter(subtype='mp4').first().download()
            self.result.text = "Download completato!"
        except Exception as e:
            self.result.text = "Errore 2"+str(e)



class YoutubeApp(App):
    def build(self):
        return InputLink()
if __name__ == "__main__":
    YoutubeApp().run()

由于是同框下载,所以没有显示更新后的文字

解决方案

  1. 将所有 yt 替换为 self.yt
  2. self.download(yt)替换为Clock.schedule_once(self.download, 1)
  3. download(self, yt) 方法替换为 download(self, dt)

片段

def click1(self, btn):
    self.result.text = self.link.text
    self.yt = ""
    # print(yt.streams.filter(only_audio=True).all())
    try:
        self.yt = YouTube(self.link.text)
        self.result.text = "Avvio il download di " + self.link.text  # <--WHY??
        print('porco')
    except Exception as e:
        self.result.text = "Errore 1" + str(e)
        return
    Clock.schedule_once(self.download, 1)

def download(self, dt):
    try:
        self.yt.streams.filter(subtype='mp4').first().download()
        self.result.text = "Download completato!"
    except Exception as e:
        self.result.text = "Errore 2" + str(e)

输出