Kivy - 另一个屏幕上的滑块 Class 值更改

Kivy - Slider Class value change on another screen

我有一个滑块,它的值改变了标签。这很容易理解。现在的问题是我想使用这个值在另一个屏幕上显示。最后,我希望滑块根据在滑块上选择的值在另一个屏幕上显示 1-16 个图像。下面是我目前所拥有的,它适用于该屏幕,但如何让这个值显示在另一个屏幕上?我知道我必须创建一个 class,它已经进行了多次尝试,但每一次似乎都让我感到困惑,而不是更好地理解它。

<NewGameScreen>:
    name: 'newgame'
    BoxLayout:
        orientation: 'vertical'
        height: 100       

        Label:
            text: 'New Game'
            font_size: 70
                Label:
                    text: 'Players'
                    font_size: 30
                    pos: 300, 400

                Slider:
                    id: s1
                    pos: 420, 400
                    width: '250dp'
                    min: 1
                    max: 16
                    step: 1
                    value: 1
                    on_value: s1.value = self.value

                Label:
                    text: '{}'.format(s1.value)
                    font_size: 30
                    pos: 670, 400

您可以通过底层 ScreenManager 传递值。一个工作示例:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen

kv = '''
<NewGameScreen>:
    name: 'newgame'
    BoxLayout:
        orientation: 'vertical'

        Slider:
            id: s1
            min: 1
            max: 16
            step: 1
            value: 1
            on_value: 
                root.manager.get_screen('options').label.text = str(self.value)
        Button:
            text: 'Options'
            on_press: 
                root.manager.transition.direction = 'left'
                root.manager.current = 'options'

<OptionScreen>:
    label: label
    name: 'options'
    orientation: 'vertical'
    BoxLayout:
        Button:
            text: 'Go back'
            on_press: 
                root.manager.transition.direction = 'right'
                root.manager.current = 'newgame'

        Label:
            id: label
            text: '1'

'''
Builder.load_string(kv)

class NewGameScreen(Screen):
    pass

class OptionScreen(Screen):
    pass

class TestApp(App):
    def build(self):
        sm = ScreenManager()
        sm.add_widget(NewGameScreen())
        sm.add_widget(OptionScreen())
        return sm

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

更新:

扩展示例(根据滑块值添加按钮):

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty

kv = '''
#:import Label kivy.uix.label.Label
<NewGameScreen>:
    name: 'newgame'
    slider: slider
    BoxLayout:
        orientation: 'vertical'

        Slider:
            id: slider
            min: 1
            max: 16
            step: 1
            value: 1
            on_value: root.update_buttons()

        Button:
            text: 'Options'
            on_press: 
                root.manager.transition.direction = 'left'
                root.manager.current = 'options'

<OptionScreen>:
    layout: layout
    name: 'options'
    BoxLayout:
        Button:
            text: 'Go back'
            on_press: 
                root.manager.transition.direction = 'right'
                root.manager.current = 'newgame'
        BoxLayout:
            orientation: 'vertical'
            id:layout
            Button:
                text: "1"

'''
Builder.load_string(kv)

class NewGameScreen(Screen):
    slider = ObjectProperty(None)
    def update_buttons(self, *args):
        layout = self.manager.get_screen('options').layout
        layout.clear_widgets()
        for i in range(int(self.slider.value)):
            layout.add_widget(Button(text=str(i+1))) 


class OptionScreen(Screen):
    layout = ObjectProperty(None)


class TestApp(App):
    def build(self):
        sm = ScreenManager()
        sm.add_widget(NewGameScreen())
        sm.add_widget(OptionScreen())
        return sm

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