简体   繁体   中英

Saving values from users choices to list in kivy

I'm new at Python and Kivy framework. I need list of user choices = user clicks on one of two pictures and based on their choice, value will be pushed into the list. But I can't manage to do it. Where should I Define function to select and how should I call it on button press. Can you help me please? edit: solved problem by this:

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

You are on the right path, but I recommend using one function in the ScreenManager instead if adding a function to each Screen , for example:

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

then in each screen you can access this function by calling root.manager.addChoice() , for example:

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

Now you will have a dictionary that will have all choices which you can access anytime you want.

Also it is better to use on_release for buttons rather than on_press to show the press animation before moving to the next screen (just to look better).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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