python - 为什么kivy不写TextArea
问题描述
我尝试了不同的解决方案,但没有一个有效。这是我最后一次尝试。
当脚本加入第一次尝试在控制台“porco”中打印但没有写入 text.result 标签时,为什么?它直接跳到最后一个标签写入完成下载
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()
解决方案
它没有显示更新的文本,因为它正在同一帧中下载。
解决方案
- 将所有出现的 替换
yt
为self.yt
- 替换
self.download(yt)
为Clock.schedule_once(self.download, 1)
- 将
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)
输出
推荐阅读
- mysql - innodb_ft_min_token_size = 1 性能影响
- postgresql - 通过 SSH Bastion 连接到 Grafana 中的 PostgreSQL 数据源
- node.js - 编写 NPM 包,我如何指定 PNG 文件路径是参考 node_module 脚本而不是从它导入的位置
- sql - SQL - 带有异常的重复日历事件
- sql-server - 使用 DBeaver 连接到 SQL Server Windows 身份验证
- php - 使用 *__in 在循环中排除当前帖子(排除没有'post__not_in')
- javascript - 如何在Javascript中将查找应用于数组?
- reactjs - 如何解决 React 中事件处理程序的丢失绑定问题?
- docker - 多个 GPU 是否可以作为一个具有更多内存的 GPU 工作?
- java - Spock 测试失败并出现 MissingPropertyException