无法让 Kivy 按钮返回它们的值

问题描述 投票:0回答:0
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
from kivy.uix.recycleview import RecycleView
from kivy.uix.button import Button
from kivy.core.window import Window
import sqlite3

#we give anime properties of name, studio, genre, year of release, we make a table with 1 column for the years, return values by SELECT * FROM anime WHERE year = x
#add a favorites screen to the app


try:
    conn = sqlite3.connect('database.db')
    cur = conn.cursor()

except:
    print('DB exists')

class MainWindow(Screen):
    def closure(self):
        conn.close()

    def table_a(self):
        cur.execute("CREATE TABLE IF NOT EXISTS anime(name text, original text, studio text, genre text, year integer)")

    def table_s(self):
        cur.execute("CREATE TABLE IF NOT EXISTS studio(id integer primary key, name text, founded integer)")

    def table_y(self):
        cur.execute("CREATE TABLE IF NOT EXISTS years(year integer)")

    def table_g(self):
        cur.execute("CREATE TABLE IF NOT EXISTS genre(id integer primary key, genre text)")

    def select(self, x):
        print(x)




class anime(Screen):

    def new(self, x):
        try:
            cur.execute("INSERT INTO anime VALUES (:name, :original, :studio, :genre, :year)",
                        {"name": x[0], "original": x[1], "studio": x[2], "genre": x[3], "year": int(x[4])})
            conn.commit()
            #for row in cur.execute("SELECT * FROM anime"):
            #    print(row)
        except:
            print("no values")

    def butts(self):
        lis = cur.execute("SELECT * FROM anime")
        l = []
        li = []
        for i in lis:
            l.append(list(i))

        for i in l:
            z = Button(text=str(i[0]), on_press=lambda x: MainWindow.select(None, i[0]))
            yield z


    def lily(self, x):
        return [x.add_widget(i) for i in self.butts()]

    def err(self):
        print("something went to shit")



class studio(Screen):

    def new(self, x):
        try:
            cur.execute("INSERT INTO studio VALUES (:id, :name, :founded)",
                        {"id": None, "name": x[0], "founded": x[1]})
            conn.commit()
            for row in cur.execute("SELECT * FROM studio"):
                print(row)
        except:
            print("no values")

    def butts(self, x):
        try:
            lis = ['a', 'a', 'a', 'a', '0', 'a', 'a', 'a', 'a', 'a', '0', 'a', 'a', 'a', 'a', 'a', '0', 'a', 'a']
            return [x.add_widget(Button(text=str(i[0]))) for i in lis]
        except:
            pass

    def err(self):
        print("something went to shit")

class years(Screen):

    try:
        def butts(self, x):
            try:
                lis = ['a', 'a', 'a', 'a', '0', 'a', 'a', 'a', 'a', 'a', '0', 'a', 'a', 'a', 'a', 'a', '0', 'a', 'a']
                return [x.add_widget(Button(text=str(i[0]))) for i in lis]
            except:
                pass
    except:
        pass

class genre(Screen):

    try:
        def butts(self, x):
            try:
                lis = ['a', 'a', 'a', 'a', '0', 'a', 'a', 'a', 'a', 'a', '0', 'a', 'a', 'a', 'a', 'a', '0', 'a', 'a']
                return [x.add_widget(Button(text=str(i[0]))) for i in lis]
            except:
                pass
    except:
        pass

class yearly(Screen):

    lis = [("no", "noo", "nooo"), ("no", "noo", "nooo"), ("no", "noo", "nooo"),
           ("no", "noo", "nooo")]


class WindowManager(ScreenManager):
    pass

kv = Builder.load_file("project.kv")

class ProjectApp(App):
    def build(self):
        return kv

if __name__ == '__main__':
    ProjectApp().run()
#:import Button kivy.uix.button.Button

WindowManager:
    MainWindow:
    anime:
    studio:
    years:
    genre:
    yearly:

<MainWindow>:
    name: "menu"

    GridLayout:
        cols: 1
        size_hint: 0.3, 1


        Button:
            text: "Anime"
            on_release:
                root.table_a()
                app.root.current = "anime"
                root.manager.transition.direction = "left"

        Button:
            text: "Studio"
            on_release:
                root.table_s()
                app.root.current = "studio"
                root.manager.transition.direction = "left"

        Button:
            text: "Godine"
            on_release:
                root.table_y()
                app.root.current = "years"
                root.manager.transition.direction = "left"

        Button:
            text: "Žanrovi"
            on_release:
                root.table_g()
                app.root.current = "genre"
                root.manager.transition.direction = "left"

        Button:
            text: "Po godinama"
            on_release:
                app.root.current = "yearly"
                root.manager.transition.direction = "left"

        Button:
            text: "Exit"
            on_release:
                app.stop()
                root.closure()

<anime>:
    name: "anime"


    GridLayout:
        cols: 2

        BoxLayout:
            id: Scroll
            orientation: 'vertical'
            spacing: 0.01
            cols: 1
            on_kv_post: root.lily(Scroll)

        GridLayout:
            cols: 1
            rows: 2

            GridLayout:
                cols: 2
                rows: 5
                spacing: 10
                padding: [10, 0, 10, 0]

                Label:
                    text: "Ime"

                TextInput:
                    id: ime

                Label:
                    text: "Original"

                TextInput:
                    id: og

                Label:
                    text: "Studio"

                TextInput:
                    id: studio

                Label:
                    text: "Žanr"

                TextInput:
                    id: genre

                Label:
                    text: "Godina"

                TextInput:
                    id: godina

            GridLayout:
                cols: 4
                rows: 1
                spacing: 10
                padding: [10, 50, 10, 50]

                Button:
                    text: "Novi"
                    on_release:
                        root.new([ime.text, og.text, studio.text, genre.text, godina.text])
                        root.lily(Scroll)



                Button:
                    text: "Spremi"



                Button:
                    text: "Obriši"


                Button:
                    text: "Go back"
                    on_release:
                        app.root.current = "menu"
                        root.manager.transition.direction = "right"


<studio>:
    name: "studio"

    GridLayout:
        cols: 2

        BoxLayout:
            id: Scroll
            orientation: 'vertical'
            spacing: 0.01
            cols: 1
            on_kv_post:
                root.butts(Scroll)

        GridLayout:
            cols: 1
            rows: 2

            GridLayout:
                cols: 2
                rows: 3

                Label:
                    text: "Studio"

                TextInput:
                    id: studio

                Label:
                    text: "Godina osnivanja"

                TextInput:
                    id: godina

            GridLayout:
                cols: 4
                rows: 1

                Button:
                    text: "Novi"
                    on_release:
                        root.new((studio.text, godina.text))


                Button:
                    text: "Spremi"


                Button:
                    text: "Obriši"


                Button:
                    text: "Go back"
                    on_release:
                        app.root.current = "menu"
                        root.manager.transition.direction = "right"

<years>:
    name: "years"

    GridLayout:
        cols: 2

        BoxLayout:
            id: Scroll
            orientation: 'vertical'
            spacing: 0.01
            cols: 1
            on_kv_post:
                root.butts(Scroll)

        GridLayout:
            cols: 1
            rows: 2

            GridLayout:
                cols: 2
                rows: 1

                Label:
                    text: "Godina"

                TextInput:
                    name: "godina"

            GridLayout:
                cols: 4
                rows: 1

                Button:
                    text: "Novi"


                Button:
                    text: "Spremi"


                Button:
                    text: "Obriši"


                Button:
                    text: "Go back"
                    on_release:
                        app.root.current = "menu"
                        root.manager.transition.direction = "right"

<genre>:
    name: "genre"

    GridLayout:
        cols: 2

        BoxLayout:
            id: Scroll
            orientation: 'vertical'
            spacing: 0.01
            cols: 1
            on_kv_post:
                root.butts(Scroll)

        GridLayout:
            cols: 1
            rows: 2

            GridLayout:
                cols: 2
                rows: 1

                Label:
                    text: "Žanr"

                TextInput:
                    name: "genre"

            GridLayout:
                cols: 4
                rows: 1

                Button:
                    text: "Novi"


                Button:
                    text: "Spremi"


                Button:
                    text: "Obriši"


                Button:
                    text: "Go back"
                    on_release:
                        app.root.current = "menu"
                        root.manager.transition.direction = "right"

<yearly>:
    name: "yearly"

    GridLayout:
        cols: 4

        GridLayout:
            cols: 1
            rows: 2

            Button:
                text: "Novi"


            Button:
                text: "Spremi"

        ScrollView:
            GridLayout:
                rows: 1000
                cols: 1
                on_kv_post: [self.add_widget(Button(text=str(i[0]))) for i in root.lis]

        GridLayout:
            cols: 1
            rows: 3

            Button:
                text: "Novi"



            Button:
                text: "Spremi"

            Button:
                text: "Go back"
                on_release:
                    app.root.current = "menu"
                    root.manager.transition.direction = "right"

        ScrollView:
            GridLayout:
                rows: 1000
                cols: 1
                on_kv_post: [self.add_widget(Button(text=str(i[0]))) for i in root.lis]

在“动漫”类中创建的按钮在按下时不会返回它们的值。当我进行更改时,它以 3 种方式结束:1.我让它们全部返回最后一个按钮的值,2.它们什么都不做,3.我得到一个错误

我尝试了很多来自互联网的建议,但都无济于事。请帮忙,此时我已经为此苦苦挣扎了一个多星期。

python python-3.x kivy kivy-language
© www.soinside.com 2019 - 2024. All rights reserved.