Search code examples
pythonclassvariablesmethodskivy

Pulling values form methods into other classes in Python


I'm writing a Python script with Kivy. As of now, the app is working as expected. Except I've hit a roadblock and have been unable to find the solution. In the "class Output(screen)" class, I'd like to pull in the values generated from the screen input users will be entering from the classes "Class Cost(Screen) and Class Tip(Screen). Right now, as the code is written, the input from the screen is being pulled into the respective classes (Cost and Tip) and is printed in Python. So, I know the logic is working. I need a way to call those values in the Class Output(Screen). Thanks in advance for any suggestions.

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




class Cost(Screen):
    cost = ObjectProperty(None)

    def btn(self):
        cost = self.ids.cost.text
        print(cost)


class Tip(Screen):
    tip = ObjectProperty(None)

    def btn(self):
        tip = self.ids.tip.text
        print(tip)


class Output(Screen):
    pass


class WindowManager(ScreenManager):
    pass


kv = Builder.load_file("att.kv")


class ATT(App):
    def build(self):
        return kv





if __name__ == "__main__":
    ATT().run()
type here

att.kv file

<Label>:
    color:0.5,0.5,0.5,1
    font_size:30

<Button>:
    background_color: 0, 0, 3, .6
    color:1.0,1.0,1.0,1
    font_size:20
    markup: True

WindowManager
    Cost:
    Tip:
    Output:

<Cost>
    name: "Cost"
    cost: cost

    Label:
        size_hint:{0.3, 0.2}
        pos_hint: {"x":0.20, "y":0.5}
        color:1.0,1.0,1.0,1
        text: "What's the Total Bill? $"

    TextInput:
        focus: "True"
        id: cost
        size_hint:{0.2, 0.1}
        pos_hint: {"x":0.58, "y":0.55}
        multiline:False
        input_filter: "float"

    Button:
        size_hint:{0.2, 0.1}
        pos_hint: {"x":0.4, "y":0.2}
        text: "[b]Next[/b]"
        on_press: root.btn()
        on_release:
            app.root.current = "Tip"
            root.manager.transition.direction = "left"

<Tip>
    name: "Tip"
    tip: tip

    Label:
        size_hint:{0.3, 0.2}
        pos_hint: {"x":0.24, "y":0.5}
        color:1.0,1.0,1.0,1
        text: "Tip Percentage? %"

    TextInput:
        id: tip
        size_hint:{0.2, 0.1}
        pos_hint: {"x":0.58, "y":0.55}
        multiline:False
        input_filter: "float"

    Button:
        size_hint:{0.2, 0.1}
        pos_hint: {"x":0.5, "y":0.2}
        text: "[b]Next[/b]"
        on_press: root.btn()
        on_release:
            app.root.current = "Output"
            root.manager.transition.direction = "left"


    Button:
        size_hint:{0.2, 0.1}
        pos_hint: {"x":0.27, "y":0.2}
        text: "[b]Back[/b]"
        on_release:
            app.root.current = "Cost"
            root.manager.transition.direction = "right"

<Output>
    name: "Output"

    Label:
        size_hint:{0.3, 0.2}
        pos_hint: {"x":0.20, "y":0.5}
        color:1.0,1.0,1.0,1
        text: "Output"


    Button:
        size_hint:{0.2, 0.1}
        pos_hint: {"x":0.5, "y":0.2}
        text: "[b]Close[/b]"
        on_press: app.stop()



    Button:
        size_hint:{0.2, 0.1}
        pos_hint: {"x":0.27, "y":0.2}
        text: "[b]Start Over[/b]"
        on_press:
            app.root.current = "Cost"
            root.manager.transition.direction = "right"


Solution

  • if you add an id to the Output class:

    <Output>
        name: "Output"
    
        Label:
            id: output
            size_hint:{0.3, 0.2}
    

    Then you can add an on_enter() method to the Output class:

    class Output(Screen):
        def on_enter(self, *args):
            self.ids.output.text = 'Cost: ' + self.manager.get_screen('Cost').ids.cost.text + ' Tip: ' + self.manager.get_screen('Tip').ids.tip.text
    

    Obviously, you can adjust the above code to do any calculations that you want.