python - Kivy,使用函数并获取随机信息到文本输入和随机图像?
问题描述
我想我迷失了解决方案。我不明白 .kv 页面的功能是如何工作的。我的示例代码如下。问题是当我单击“祝你好运”按钮时出现错误,例如“AttributeError:'Button'对象没有属性'app'”我是否遗漏了一些明显的东西,或者我的整个方式都是错误的?
主文件
# -*- coding: utf8 -*-
from trakt.users import User
import random
import imdb
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
import kivy.app
kivy.require('1.9.1')
moviesDB = imdb.IMDb()
myuser = User(str('lunedor'))
watchlist = myuser.watchlist_movies
def dataget():
global myline
myline = str(random.choice(watchlist))[9:1000]
movies = moviesDB.search_movie(str(myline))
id = movies[0].getID()
global movie
movie = moviesDB.get_movie(id)
global posterurl
posterurl = movie["full-size cover url"]
global title
title = movie['title']
global year
year = movie["year"]
global rating
rating = movie["rating"]
global runtime
runtimes = movie["runtimes"]
runtime = ' '.join(map(str, runtimes))
global directStr
directors = movie["directors"]
directStr = ' '.join(map(str, directors))
global writerStr
writers = movie["writers"]
writerStr = ', '.join(map(str, writers))
global casting
casting = movie["cast"]
global actors
actors = ', '.join(map(str, casting))
global summary
summary = movie["plot outline"]
genres = movie["genres"]
global genre
genre = ', '.join(map(str, genres))
print(movie)
print(id)
posterurl = movie["full-size cover url"]
showline1 = title + "\n" + str(year) + " - " + str(rating) + "\n" + str(runtime) + " minutes" + " - " + genre
showline2 = "Director: " + directStr + "\n" + "\n" + "Writers: " + writerStr
showline3 = "Cast: " + actors
showline4 = "Summary: " + "\n" + str(summary)
class RootWidget(GridLayout):
def __init__(self, **kwargs):
super(RootWidget, self).__init__(**kwargs)
try:
dataget()
except:
print('Error')
dataget()
posterurl = movie["full-size cover url"]
showline1 = title + "\n" + str(year) + " - " + str(rating) + "\n" + str(runtime) + " minutes" + " - " + genre
showline2 = "Director: " + directStr + "\n" + "\n" + "Writers: " + writerStr
showline3 = "Cast: " + actors
showline4 = "Summary: " + "\n" + str(summary)
class MyApp(App):
def build(self):
return RootWidget()
def clk(self):
try:
dataget()
except:
print('Error')
dataget()
def clk2(self):
exit()
if __name__ == '__main__':
App=MyApp()
App.run()
我的.kv
<RootWidget>:
cols: 2
rows: 1
orientation: 'horizontal'
GridLayout:
cols: 1
rows: 1
orientation: 'horizontal'
AsyncImage:
id: poster
source: root.posterurl
nocache: True
GridLayout:
cols: 1
rows: 5
orientation: 'vertical'
spacing: 10
Label:
text: root.showline1
markup: True
bold: True
size: self.texture_size
halign: 'center'
valign: 'middle'
id: self.lbl
TextInput:
text: root.showline2
id: self.lbl2
TextInput:
text: root.showline3
id: self.lbl3
multiline: True
TextInput:
text: root.showline4
id: self.lbl4
multiline: True
BoxLayout:
cols: 2
rows: 1
spacing: 5
padding: 50, 10, 10, 10
size_hint_max_y: 40
orientation: 'horizontal'
Button:
text: "Good Luck!"
font_size: 14
size_hint: None, None
size: 95, 30
on_press: root.app.clk()
Button:
text: "Exit"
font_size: 14
size_hint: None, None
size: 75, 30
on_press: root.app.clk2()
解决版本:
# -*- coding: utf8 -*-
from trakt.users import User
import random
import imdb
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.properties import StringProperty, ObjectProperty
import kivy.app
kivy.require('1.9.1')
moviesDB = imdb.IMDb()
myuser = User(str('lunedor'))
watchlist = myuser.watchlist_movies
def dataget():
global myline
myline = str(random.choice(watchlist))[9:1000]
movies = moviesDB.search_movie(str(myline))
id = movies[0].getID()
global movie
movie = moviesDB.get_movie(id)
global posterurl
posterurl = movie["full-size cover url"]
global title
title = movie['title']
global year
year = movie["year"]
global rating
rating = movie["rating"]
global runtime
runtimes = movie["runtimes"]
runtime = ' '.join(map(str, runtimes))
global directStr
directors = movie["directors"]
directStr = ' '.join(map(str, directors))
global writerStr
writers = movie["writers"]
writerStr = ', '.join(map(str, writers))
global casting
casting = movie["cast"]
global actors
actors = ', '.join(map(str, casting))
global summary
summary = movie["plot outline"]
genres = movie["genres"]
global genre
genre = ', '.join(map(str, genres))
print(movie)
print(id)
class RootWidget(GridLayout):
def __init__(self, **kwargs):
super(RootWidget, self).__init__(**kwargs)
try:
dataget()
except:
print('Error')
dataget()
posterurl = ObjectProperty()
showline1 = StringProperty()
showline2 = StringProperty()
showline3 = StringProperty()
showline4 = StringProperty()
# make a function to be able to call it
def update_values(self):
self.posterurl = movie["full-size cover url"]
self.showline1 = title + "\n" + str(year) + " - " + str(rating) + "\n" + str(runtime) + " minutes" + " - " + genre
self.showline2 = "Director: " + directStr + "\n" + "\n" + "Writers: " + writerStr
self.showline3 = "Cast: " + actors
self.showline4 = "Summary: " + "\n" + str(summary)
class MyApp(App):
def build(self):
# you will need that object later so put in into variable
self.rw = RootWidget()
# call update function
self.rw.update_values()
return self.rw
def clk(self):
try:
dataget()
# after you get new values update the variables connected to widgets
self.rw.update_values()
except:
print('Error')
dataget()
def clk2(self):
exit()
if __name__ == '__main__':
App=MyApp()
App.run()
解决方案
您应该使用 root 或应用程序,而不是同时使用两者。在您的情况下,它将是 app 因为您的函数在 App 类中:
on_press: app.clk()
当然它不会自己更新,你更新全局变量但不更新本地变量。尝试这个:
class RootWidget(GridLayout):
def __init__(self, **kwargs):
super(RootWidget, self).__init__(**kwargs)
try:
dataget()
except:
print('Error')
dataget()
# make a function to be able to call it
def update_values(self):
self.posterurl = movie["full-size cover url"]
self.showline1 = title + "\n" + str(year) + " - " + str(rating) + "\n" + str(runtime) + " minutes" + " - " + genre
self.showline2 = "Director: " + directStr + "\n" + "\n" + "Writers: " + writerStr
self.showline3 = "Cast: " + actors
self.showline4 = "Summary: " + "\n" + str(summary)
class MyApp(App):
def build(self):
# you will need that object later so put in into variable
self.rw = RootWidget()
# call update function
self.rw.update_values()
return self.rw
def clk(self):
try:
dataget()
# after you get new values update the variables connected to widgets
self.rw.update_values()
except:
print('Error')
dataget()
这应该有效。如果它不会,我会告诉你艰难的方式。
推荐阅读
- python - Python代码输出不显示条形图可视化
- python - CNN Pytorch 仅支持空间目标批次的错误
- javascript - Vue JS Make下拉选择在选择选项时运行查询
- asp.net - 文件名的 Ajax 工具包问题(文件名中的非法字符)
- php - 这是 Json 数据、XML 还是数组?
- javascript - 在 Mongoose 中引用另一个 Schema
- laravel - Laravel,修改集合只保存最后一个值
- firebase - 为什么我的用户详细信息没有保存到实时数据库中?
- json - 如何仅提取 JSON 的一部分并在 .netcore 中对其进行反序列化
- gitlab - 带有令牌的 Gitlab CI 管道