繁体   English   中英

保存用户选择中的值以在kivy中列出

[英]Saving values from users choices to list in kivy

我是Python和Kivy框架的新手。 我需要用户选择列表=用户单击两张图片之一,然后根据他们的选择,值将被推入列表中。 但是我做不到。 我应该在哪里定义要选择的功能,以及如何在按下按钮时调用它。 你能帮我吗? 编辑:通过以下方式解决问题:

file.py

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image
from kivy.uix.behaviors import ButtonBehavior
from kivy.properties import ListProperty
from kivy.uix.listview import ListItemButton


class ScreenOne(Screen):
    pass

class ScreenTwo(Screen):
    def add_genre(self, lang):
        app = App.get_running_app()
        app.MY_LANG = lang

class ScreenThree(Screen):
    def add_genre(self, *argv):
        app = App.get_running_app()
        for n in argv:
            app.MY_DATA.append(n)

class ScreenFour(Screen):
    def add_genre(self, gen):
        app = App.get_running_app()
        app.MY_DATA.append(gen)

class ScreenFive(Screen):
    def press_readLang(self):
        app = App.get_running_app()
        self.ids.lbl1.text = "SharedVar is " + app.MY_LANG

    def press_read(self):
        app = App.get_running_app()
        self.ids.lbl1.text = "SharedVar is " + ', '.join(app.MY_DATA)

class ScreenSix(Screen):
    pass

class ScreenSeven(Screen):
    pass

class ImageButton(ButtonBehavior, Image, BoxLayout):
    pass



class Filmy(ScreenManager):
    screen_one = ObjectProperty(None)
    screen_two = ObjectProperty(None)
    screen_three = ObjectProperty(None)
    screen_four = ObjectProperty(None)
    screen_five = ObjectProperty(None)

class FilmyApp(App):
    MY_DATA = []
    MY_LANG = ''
    MY_DATE = ''

    def build(self):
        return Filmy()



filmy = FilmyApp()
filmy.run()

file.kv

#: import main filmy
#: import ListAdapter kivy.adapters.listadapter.ListAdapter
#: import ListItemButton kivy.uix.listview.ListItemButton


<ScreenOne>:
    BoxLayout:
        Label:
            text: "Welcome to Random Movie.\nYou will see several couples of picture. \nLet yourself be emotional and choose one.\nAfter that application chooses you 3 random movies. "
        Button:
            text: "Start"
            on_press: root.manager.current = 'screen2'


<ScreenTwo>:
    BoxLayout:
        ImageButton:
            #cizojazycne: cizojazycne
            #id:cizojazycne
            on_press:
                root.manager.current = 'screen3'
                root.add_genre('en')
            source: "./zkouska.jpg"
            keep_ratio: False
            allow_stretch: True
        ImageButton:
            on_press:
                root.manager.current = 'screen3'
                root.add_genre('cz')
            source: "./zkouska.jpg"
            keep_ratio: False
            allow_stretch: True


<ScreenThree>:
    BoxLayout:
        ImageButton:
            on_press:
                root.manager.current = 'screen4'
                root.add_genre('35', '66', '44')
            source: "./zkouska.jpg"
            keep_ratio: False
            allow_stretch: True
        ImageButton:
            on_press:
                root.manager.current = 'screen4'
                root.add_genre('35', '66', '44')
                root.add_genre('dwadwad')
            source: "./zkouska.jpg"
            keep_ratio: False
            allow_stretch: True

<ScreenFour>:
    BoxLayout:
        ImageButton:
            on_press:
                root.manager.current = 'screen5'
                root.add_genre('1751')
            source: "./zkouska.jpg"
            keep_ratio: False
            allow_stretch: True
        ImageButton:
            on_press:
                root.manager.current = 'screen5'
                root.add_genre('4')
            source: "./zkouska.jpg"
            keep_ratio: False
            allow_stretch: True

<ScreenFive>
    BoxLayout:
        orientation: "vertical"

        Label:
            id: lbl1

        Button:
            text: "Film 1"
        Button:
            text: "Film 2"
        Button:
            text: "Film 3"
            on_press: root.press_read()
        Button:
            text: "Try again"
            on_press: root.manager.current = 'screen1'



<Filmy>:
    id: screen_manager

    screen_one: screen_one
    screen_two: screen_two
    screen_three: screen_three
    screen_four: screen_four
    screen_five: screen_five

    ScreenOne:
        id: screen_one
        name: 'screen1'
        manager: screen_manager

    ScreenTwo:
        id: screen_two
        name: 'screen2'
        manager: screen_manager

    ScreenThree:
        id: screen_three
        name: 'screen3'
        manager: screen_manager

    ScreenFour:
        id: screen_four
        name: 'screen4'
        manager: screen_manager

    ScreenFive:
        id: screen_five
        name: 'screen5'
        manager: screen_manager

您所走的路是正确的,但是如果向每个Screen添加功能,则建议在ScreenManager使用一个功能,例如:

class Filmy(ScreenManager):
    screen_one = ObjectProperty(None) # You don't need those ObjectProperty variables
    screen_two = ObjectProperty(None) # so you can delete all those
    screen_three = ObjectProperty(None)
    screen_four = ObjectProperty(None)
    screen_five = ObjectProperty(None)

    choices = {}

    @staticmethod
    def addChoice(key, value):
        choices[key] = value

然后在每个屏幕中,您可以通过调用root.manager.addChoice()来访问此函数,例如:

ImageButton:
    #cizojazycne: cizojazycne    
    #id:cizojazycne
    on_press:
        root.manager.current = 'screen3'
        root.manager.addChoice('MY_LANG', 'en')
    source: "./zkouska.jpg"
    keep_ratio: False
    allow_stretch: True

ImageButton:
    on_press:
        root.manager.current = 'screen3'
        root.manager.addChoice('MY_LANG', 'cz')
    source: "./zkouska.jpg"
    keep_ratio: False
    allow_stretch: True

现在,您将拥有一dictionary ,其中包含您可以随时访问的所有选择。

另外,最好将on_release用于按钮,而不要使用on_press来显示新闻动画,然后再移至下一个屏幕(以看起来更好)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM