Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 保存用户选择中的值以在kivy中列出_Python_Kivy - Fatal编程技术网

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

Python 保存用户选择中的值以在kivy中列出,python,kivy,Python,Kivy,我是Python和Kivy框架的新手。我需要用户选择列表=用户单击两张图片中的一张,根据他们的选择,值将被推送到列表中。但是我做不到。我应该在哪里定义要选择的函数,以及如何在按下按钮时调用它。你能帮我吗?编辑:通过以下方式解决问题: file.py from kivy.app import App from kivy.uix.screenmanager import ScreenManager, Screen from kivy.properties import ObjectProperty

我是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

您走的是正确的道路,但如果在每个屏幕上添加一个函数,我建议您在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
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
然后,在每个屏幕中,您可以通过调用root.manager.addChoice访问此函数,例如:

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
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
现在,您将拥有一本字典,其中包含您可以随时访问的所有选项


此外,最好在移动到下一个屏幕之前,使用按钮的on_release(打开释放)而不是on_press(按下)来显示按键动画,以便看起来更好。

您能解释更多吗?点击了哪些图片?您希望函数做什么?我正在尝试,但我的英语能力有限:。这是我的第一份申请。逐步:应用程序有3个屏幕,每个屏幕有两张图片。在每个屏幕上选择一张图片。每一张图片都代表标准语言:第一张图片cz,第二张en,年份:第一张到1990年,第二张到1990年,类型:第一部喜剧,家庭等,第二部恐怖片,惊悚片等。。用户不知道他选择的是哪个标准,他只看到图片。图片中的值存储到列表中。根据列表,进行api调用以检索电影。我解决了这个问题。但我对结构不太确定。如果您觉得答案有用,请单击答案左侧投票下方的勾号,不要忘记接受答案:-这是我关于StackOverFlow的第一个问题:。谢谢你的提醒。我们很高兴这么做,很乐意帮助你=