KIVY:如何使用 on_touch_down 进入第二个屏幕?

KIVY: How to go to second screen using on_touch_down?

到目前为止,我有简单的滚动视图和一个范围为 10 的 for 循环来表示一些数据,稍后我会添加,数据在 MDCard 内。我希望用户能够点击 10 张卡片中的任何一张,然后将它们带到我在 Kivy 中创建的第二个屏幕并显示用户点击的数字。

我已经使用 on_touch_down 来注册用户点击卡片,但我不确定如何将用户引导到显示他们点击的号码的第二个屏幕。

如何在 Python 中创建函数来执行此操作?

*.py

import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.properties import StringProperty
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.clock import Clock
from kivy.uix.button import Button
from kivy.core.window import Window
from kivymd.app import MDApp
from kivymd.uix.card import MDCard
from kivymd.uix.label import MDLabel


class FirstWindow(Screen):
    def __init__(self, **kwargs):
        super(FirstWindow, self).__init__(**kwargs)
        Clock.schedule_once(self.create_scrollview)

    def create_scrollview(self, dt):
        layout = BoxLayout( orientation='vertical', spacing=25, size_hint_y=None, size_hint=(1, None), padding=[170, 10])
        layout.bind(minimum_height=layout.setter("height"))

        for self.x in range(10):

            card = MDCard(orientation='vertical', size_hint=(1, None), height=200 ,padding=(20,0),ripple_behavior= True,radius=[5, ])
            card.add_widget(MDLabel(text=str(self.x), halign= "center"))
            layout.add_widget(card)

            card.bind(on_touch_down=self.clicked) 

        scrollview = ScrollView(size_hint=(1, 1))
        self.view.add_widget(scrollview)
        scrollview.add_widget(layout)
    
    def clicked(self, card, touch):
        self.create_scrollview
        if card.collide_point(*touch.pos):
            print(self.x)

        
class SecondWindow(Screen):
    pass

class WindowManager(ScreenManager):
    pass



class NearMeApp(MDApp):
    def build(self):
        self.theme_cls.theme_style ="Dark"
        self.theme_cls.accent_palette = "Red"
        self.theme_cls.primary_palette = "Blue"
        self.root = Builder.load_file('NearMe.kv')

if __name__ == '__main__':
    NearMeApp().run()

*.kv

WindowManager:
    FirstWindow:
    SecondWindow:
<FirstWindow>:
    name:"FirstWindow"
    view: view
    BoxLayout:
        orientation: 'vertical'
        canvas.before:
            Color:
                rgba: .2, .2, .2, 1
            Rectangle:
                pos: self.pos
                size: self.size
        BoxLayout:
            orientation: 'vertical'
        
            BoxLayout:
                size: (50, 50)
                size_hint: (1, None)
                Label:
                    text: "xxx"
                    canvas.before:
                        Color:
                            rgba: 0, 1, 1, 1
        
            BoxLayout:
                size: (50, 50)
                size_hint: (1, None)
                Label:
                    text:"xxx"
                    canvas.before:
                        Color:
                            rgba: 1, 1, 1, 1
        
            ScrollView:
                id: view 
                canvas.before:
                    Color:
                        rgba: 1, 1, 0, 1

<SecondWindow>:
    name:"SecondWindow"
    BoxLayout:
        orientation: 'vertical'
        canvas.before:
            Color:
                rgba: .2, .2, .2, 1
            Rectangle:
                pos: self.pos
                size: self.size
        BoxLayout:
            orientation: 'vertical'
            Label:
                text: "Screen2"
            Button:
                text:"click"
                on_release:
                    app.root.current = "FirstWindow"

            

我认为你只需要修改你的clicked()方法:

def clicked(self, card, touch):
    if card.collide_point(*touch.pos):
        print(card.children[0].text)
        self.manager.current = 'SecondWindow'