[英]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.