How to access values in kivy python classes from different screens?

Question:

I have the following basic python file that consumes a number and returns the number * 10.

class SomeOperation:
    
    def __init__(self, input_num=100):
        self.input_num = input_num
    
    def do_something(self):
        print(self.input_num * 10)
        return self.input_num * 10

if __name__ == '__main__':
    q = SomeOperation()
    q.do_something()

I would like to call this script in the PlaySetUp screen of the below app.
Here a slider will determine input_num and also have a play button that will take me to the PlayScreen of the app.

from kivy.app import App

from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.uix.slider import Slider
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen

from someoperation import SomeOperation

class WindowManager(ScreenManager):
    def __init__(self, **kwargs):
        super(WindowManager, self).__init__(**kwargs)

class MainWindow(Screen):
    def __init__(self, **kwargs):
        super(MainWindow, self).__init__(**kwargs)

class PlaySetUp(Screen):
    def __init__(self, *args, **kwargs):
        super(PlaySetUp, self).__init__(*args, **kwargs)
        
class PlayScreen(Screen, SomeOperation):
    def __init__(self, *args, **kwargs):
        super(PlayScreen, self).__init__(*args, **kwargs)

class NumberSelection(BoxLayout):
     
    def __init__(self, *args, **kwargs):
        super(NumberSelection, self).__init__(*args, **kwargs)
        self.orientation = 'vertical'
        self.input_num = 10
        
        self.input_num_value = Label(text='10')
        self.add_widget(self.input_num_value)
        
        self.number_slider = Slider(min=10, max=100, step=10)
        self.number_slider.bind(value = self.on_value)        
        self.add_widget(self.number_slider)

    def on_value(self, widget, value):
        self.input_num = value
        self.input_num_value.text = str(value)

kv = Builder.load_file('sample.kv')

class SomeApp(App):
    def __init__(self, **kwargs):
        super(SomeApp, self).__init__(**kwargs)
    
    def build(self):
        return kv

The kv file:

WindowManager:
    MainWindow:
    PlaySetUp:
    PlayScreen:
        id: play_screen

<MainWindow>:
    name: "main"
    BoxLayout:
        orientation: "vertical"
        Button:
            text: "Play"
            on_release: 
                app.root.current = "play_set_up"
                root.manager.transition.direction = "left"

<PlaySetUp>:
    name: "play_set_up"
    BoxLayout:
        orientation: "vertical"
        NumberSelection:
        Button:
            text: "Go!"
            on_release: 
                app.root.current = "play_screen"
                root.manager.transition.direction = "left"
        Button:
            text: "Back!"
            on_release: 
                app.root.current = "main"
                root.manager.transition.direction = "right"        
        
                
<PlayScreen>:
    name: "play_screen"
    BoxLayout:
        orientation: "vertical"
        Label:
            text: #TODO: show the output of SomeOperation
        Button:
            text: "Run"
            on_release: #TODO: re-run SomeOperation with same set up as in the PlaySetUp screen
        Button:
            text: "Back!"
            on_release: 
                app.root.current = "play_set_up"
                root.manager.transition.direction = "right"  

I would like to run the SomeOperation script automatically in the PlayScreen
plus re-run it every time I press the Run button. The result should always be the same unless I press the back button and change the slider value in the PlaySetUp screen.

Thanks in advance!

Asked By: RealRageDontQuit

||

Answers:

Figured out that you can set variable names in the build function of the App.

class SomeApp(App):
    my_int = NumericProperty(0)
    def __init__(self, **kwargs):
        super(SomeApp, self).__init__(**kwargs)
        
    def build(self):
        return kv

then the function for the slider can set the value with:

def on_value(self, widget, value):
    self.input_num = value
    self.input_num_value.text = str(value)
    SomeApp.my_int = value

This way all other functions can subsequently call this new value

Answered By: RealRageDontQuit
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.