Skip to content
Advertisement

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

output KivyMD Programmers, im new in KivyMD….

on_start() list items are sucessfully showing and on_press sending a selected value too passValue() function….

but here now i wants open new Screen under passValue() function…and pass variable value’s to new Screen….

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()

Advertisement

Answer

Since It’s not clear exactly where in your screen Bye you want some variable to be passed, let’s suppose you want to pass a list’s text to the MDLabel having text ‘Good Bye’ of the screen Bye by method passValue.

You may achieve this as follows:

First in the kvlang of screen Bye assign an id say, target to the MDLabel,

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

Then in your method 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

As a side note you perhaps don’t need (as you already defined that in kvlang) the following:

sm = ScreenManager()
sm.add_widget(Hello(name = 'hello'))
sm.add_widget(Bye(name = 'bye'))
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement