6

I have a slider which value changes the label. That was easy to understand. Now the problem is I want to use this value to show on another screen. Eventually I want the slider to show 1-16 images on another screen dependent on what value is selected on the slider. Below is what I have as of now, which works well for that screen, but how do I get this value to show on another screen? I know I have to create a class, which have made multiple attempts and each seems to confuse me more than understand it better.

<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

1 Answer 1

11

You can pass values through underlying ScreenManager. A working example:

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

Update:

An extended exaple (adding buttons based on slider value):

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()
6
  • Ok, but what about if I wanted that value from the slider to become 1-16 buttons on options screen? Would I leave that as a value then?
    – tunedin
    Commented Apr 2, 2015 at 19:07
  • But thanks for the demo and explanation, really helps
    – tunedin
    Commented Apr 2, 2015 at 19:10
  • I'm afraid that i don't really understand what you mean. Can you explain?
    – Nykakin
    Commented Apr 2, 2015 at 19:47
  • So the value that the slider creates, could be 1-16, will create the amount of buttons shown on options screen. This is creating how many players for the game, and want the "Options Screen" to display a grid layout and buttons to later select other options. Hope that helps. Another thing is the value from the Slider will also create db but that is later once I get more experience
    – tunedin
    Commented Apr 2, 2015 at 19:55
  • @tunedin, I added another example. I hope that helps.
    – Nykakin
    Commented Apr 2, 2015 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.