如何在 KivyMD 中将仅选定的值发送到另一个屏幕?

How to send Only Selected value to another screen in KivyMD?

output KivyMD 程序员,我是 KivyMD 的新手....

on_start() 列表项已成功显示并且 on_press 也发送选定值 passValue() 函数....

但现在我想在 passValue() 函数下打开新屏幕...并将变量值传递给新屏幕....

from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen,ScreenManager
from kivy.lang import Builder
from kivymd.uix.list import OneLineListItem

#Builder String
helper_string = '''
ScreenManager:
    Hello:
    Bye:
<Hello>:
    name: 'hello'
    ScrollView:
        MDList:
            id: list
            
<Bye>:
    name: 'bye'
    MDLabel:        
        text:'Good Bye'
    MDLabel:
        id:'aaa'
        text:""

'''
class Hello(Screen):
    pass
class Bye(Screen):
    pass
sm = ScreenManager()
sm.add_widget(Hello(name = 'hello'))
sm.add_widget(Bye(name = 'bye'))


class DemoApp(MDApp):
    def build(self):
        screen = Screen()

        self.help_str = Builder.load_string(helper_string)

        screen.add_widget(self.help_str)
        return screen

    def on_start(self):
        for i in range(50):        
            item = OneLineListItem(text='Item ' + str(i),            
            on_release=lambda x, value_for_pass={i}: self.passValue(value_for_pass)
            )                                    
            self.help_str.get_screen('hello').ids.list.add_widget(item)
        

    def passValue(self, *args): 
        args_str = ','.join(map(str,args))
        print(args_str)        
        self.help_str.get_screen('bye').manager.current = 'bye' #how to pass/send args_str's value to bye screen???

DemoApp().run()

由于不清楚您希望传递某些变量在屏幕 Bye 中的确切位置,因此假设您要将列表的 text 传递给具有 text 'Good Bye' 屏幕 Bye 通过方法 passValue.

您可以通过以下方式实现:

首先在屏幕 kvlangBye 分配一个 id 说,targetMDLabel

<Bye>:
    name: 'bye'
    MDLabel:
        id: target
        text:'Good Bye'

然后在你的方法中passValue,

    def passValue(self, *args): 
        args_str = ','.join(map(str,args))
        print(args_str)
        bye_screen = self.help_str.get_screen('bye')
        bye_screen.manager.current = 'bye'
        bye_screen.ids.target.text = args_str

附带说明一下,您可能不需要(正如您已经在 kvlang 中定义的那样)以下内容:

sm = ScreenManager()
sm.add_widget(Hello(name = 'hello'))
sm.add_widget(Bye(name = 'bye'))