我是Python和Kivy框架的新手。我需要用户选择列表=用户点击两张图片中的一张并根据他们的选择,价值将被推入列表中。但我无法做到这一点。我应该在哪里定义要选择的功能以及如何在按下按钮时调用它。你能帮我吗?编辑:解决问题:
file.py
about_path
file.kv
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()
答案 0 :(得分:0)
您走在正确的道路上,但我建议在ScreenManager
中使用一个功能,而不是向每个Screen
添加一个功能,例如:
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
来显示新闻动画,然后再转到下一个屏幕(只是为了看起来更好)。