我在我的应用程序的Flashcard屏幕上制作了10个按钮,所有这些按钮都指向flashDisplay屏幕on_press。因此,在FlashDisplay屏幕中,我需要知道哪个按钮已被按下,以便我可以相应地触发事件。我怎样才能做到这一点?是否有按钮的任何属性给出按钮的索引或我们必须使用id。以及如何做到这一点?
这里是kv代码的一部分 -
<FlashCard>:
ScrollView:
size: self.size
GridLayout:
cols: 5
rows: 2
padding:10
spacing:10
Button:
text:'Flash Card 1'
on_press: root.manager.current='flash_display'
width:200
size_hint_x: None
Button:
text:'Flash Card 2'
on_press: root.manager.current='flash_display'
width:200
size_hint_x: None
Button:
text:'Flash Card 3'
on_press: root.manager.current='flash_display'
width:200
size_hint_x: None
Button:
text:'Flash Card 4'
on_press: root.manager.current='flash_display'
width:200
size_hint_x: None
Button:
text:'Flash Card 5'
on_press: root.manager.current='flash_display'
width:200
size_hint_x: None
Button:
text:'Flash Card 6'
on_press: root.manager.current='flash_display'
width:200
size_hint_x: None
Button:
text:'Flash Card 7'
on_press: root.manager.current='flash_display'
width:200
size_hint_x: None
Button:
text:'Flash Card 8'
on_press: root.manager.current='flash_display'
width:200
size_hint_x: None
Button:
text:'Flash Card 9'
on_press: root.manager.current='flash_display'
width:200
size_hint_x: None
Button:
text:'Flash Card 10'
on_press: root.manager.current='flash_display'
width:200
size_hint_x: None
py代码 -
class FlashDisplayPage(Screen): #name of this screen is flash_display
# code for indexing the button
# other codes
答案 0 :(得分:1)
该属性不是默认属性,因此必须创建它。而不是使用root.manager.current = 'flash_display'
,你应该调用另一个传递索引并更改屏幕的函数。要设置索引,我们可以使用setattr
来创建新属性,然后我们可以通过self.index
获取该索引。
<强> test.kv 强>
<FlashCard>:
ScrollView:
size: self.size
GridLayout:
cols: 5
rows: 2
padding:10
spacing:10
Button:
text:'Flash Card 1'
on_press: root.on_press(1)
width:200
size_hint_x: None
Button:
text:'Flash Card 2'
on_press: root.on_press(2)
width:200
size_hint_x: None
Button:
text:'Flash Card 3'
on_press: root.on_press(3)
width:200
size_hint_x: None
Button:
text:'Flash Card 4'
on_press: root.on_press(4)
width:200
size_hint_x: None
Button:
text:'Flash Card 5'
on_press: root.on_press(5)
width:200
size_hint_x: None
Button:
text:'Flash Card 6'
on_press: root.on_press(6)
width:200
size_hint_x: None
Button:
text:'Flash Card 7'
on_press: root.on_press(7)
width:200
size_hint_x: None
Button:
text:'Flash Card 8'
on_press: root.on_press(8)
width:200
size_hint_x: None
Button:
text:'Flash Card 9'
on_press: root.on_press(9)
width:200
size_hint_x: None
Button:
text:'Flash Card 10'
on_press: root.on_press(10)
width:200
size_hint_x: None
<强> main.py 强>
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
class FLashCard(Screen):
def on_press(self, index):
flash_display_screen = self.manager.get_screen('flash_display')
setattr(flash_display_screen, 'index', index)
self.manager.current='flash_display'
class FlashDisplayPage(Screen):
def on_pre_enter(self):
print("on_pre_enter",self.index)
def on_enter(self):
print("on_enter", self.index)
class TestApp(App):
def build(self):
sm = ScreenManager()
sm.add_widget(FLashCard(name="flasg_card"))
sm.add_widget(FlashDisplayPage(name="flash_display"))
return sm
if __name__ == '__main__':
TestApp().run()