Kivy,使用函数并将随机信息获取到文本输入和随机图像?

我想我迷失了解决方案。我不明白如何使用.kv页面使用该函数。我的示例代码如下。问题是,当我点击“好运”按钮时,我收到一个错误,例如“属性错误:'按钮'对象没有属性'app'”我尝试将我的函数行替换为不同的地方,例如在类下,但保持得到不同的错误,我是否错过了明显的东西,或者只是我的整个方式都是错误的?


main.py


# -*- 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()


慕侠2389804
浏览 86回答 1
1回答

弑天下

您应该使用 root 或应用程序,而不是同时使用两者。在你的例子中,它将是应用程序,因为你的函数在应用程序类中: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()这应该有效。如果它不会,我会告诉你艰难的方式。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python