为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕侠2389804 2022-08-02 18:17:31
我想我迷失了解决方案。我不明白如何使用.kv页面使用该函数。我的示例代码如下。问题是,当我点击“好运”按钮时,我收到一个错误,例如“属性错误:'按钮'对象没有属性'app'”我尝试将我的函数行替换为不同的地方,例如在类下,但保持得到不同的错误,我是否错过了明显的东西,或者只是我的整个方式都是错误的?main.py# -*- coding: utf8 -*-from trakt.users import Userimport randomimport imdbfrom kivy.app import Appfrom kivy.uix.gridlayout import GridLayoutimport kivy.appkivy.require('1.9.1')moviesDB = imdb.IMDb()myuser = User(str('lunedor'))watchlist = myuser.watchlist_moviesdef 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()
查看完整描述

1 回答

?
弑天下

TA贡献1818条经验 获得超8个赞

您应该使用 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()

这应该有效。如果它不会,我会告诉你艰难的方式。


查看完整回答
反对 回复 2022-08-02
  • 1 回答
  • 0 关注
  • 85 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信