lundi 17 février 2020

Kivy, using functions and getting random info to textinput and random image?

I think I am lost for solution. I didn't get how works the function with .kv pages. My sample codes are below. Problem is when I clicked to "Good Luck" button I get an error such as "AttributeError: 'Button' object has no attribute 'app'" I tried replace my function lines to different places such as under class but keep get different errors, Am I missing something obvious or just my whole way is wrong?

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"]
    ##Metin Ayarları
    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"]
    ##Metin Ayarları
    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()

My.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()



Aucun commentaire:

Enregistrer un commentaire