Chaos21
Chaos21

Reputation: 5

How to reference other class methods in kivy

I'm new to stack overflow but I've been programming in python for a couple years. One thing I havent done much of is object oriented programming. I just started learning kivy and I think I might have screwed up the root of my program by organizing it wrong. I used classes to define each label, button, layout...etc. now I'm wondering how I can reference the attributes of each text input to use what's filled in for other methods I want to define.

Example: have a button when pressed gets the input from two different text inputs and adds them together and a label displays it.

You can see what I've tried in my code but I'm having a hell of a time figuring out how to reference all these things from different classes.

I hope my code is understandable...sorry its lengthy but I want you to see the whole scope of what I did. I'm really hoping I can get a solution to where I can keep my organization and have certain things only show up under their own files but I understand if I have to change a lot of things...

Main.py

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.uix.gridlayout import GridLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.togglebutton import ToggleButton
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from os import listdir
from kivy.core.window import Window

#load all files with 'kv' in folder
kv_path = './kv/'
for kv in listdir(kv_path):
    Builder.load_file(kv_path+kv)

#move the keyboard below text input
Window.softinput_mode = 'below_target'



#classes for savings and loan screens
class SaveButton(Button):
    pass

class LoanButton(Button):
    pass

class Calculate(Button):
    def add(self):
        total = SaveDepositInput.inideposit +GoalAmountInput.amount
        return total
    pass

class TotalsLabel(Label):
    pass

#layout classes                  
class MainBoxLayout(BoxLayout):
    pass

class InsideAnchorLayout(AnchorLayout):
    pass

class OneColGridlayout(GridLayout):
    pass

class AColGridLayout(GridLayout):
    pass

class TwoColGridLayout(GridLayout):
    pass

class WidgetRelativeLayout(RelativeLayout):
    pass



#Toggle Buttons
class DailyToggle(ToggleButton):
    pass

class WeeklyToggle(ToggleButton):
    pass

class BiWeeklyToggle(ToggleButton):
    pass

class MonthlyToggle(ToggleButton):
    pass

class YearlyToggle(ToggleButton):
    pass

class NoneToggle(ToggleButton):
    pass

class Monthly2Toggle(ToggleButton):
    pass

class Yearly2Toggle(ToggleButton):
    pass


#classes for screen change
class SaveScreen(Screen):
    pass

class LoanScreen(Screen):
    pass

class SaveLoanTabs(TabbedPanel):
    pass



#classes for savings screen
class OutputLabel(Label):
    pass

class GoalOutputLabel(Label):
    pass

class NoReinvestLabel(Label):
    pass

class SaveInstructLabel(Label):
    pass

class SaveDepositLabel(Label):
    pass

class SaveDepositInput(TextInput):
    def inideposit(self):
        initial = root.TextInput.text
        deposit = int(initial)
        return deposit
    pass

class SaveYearsLabel(Label):
    pass    

class SaveYearsInput(TextInput):
    pass

class SaveMonthsInput(TextInput):
    pass

class ChooseCompoundLabel(Label):
    pass

class SaveInterestLabel(Label):
    pass

class SaveInterestInput(TextInput):
    pass

class RepeatDepositLabel(Label):
    pass

class RepeatDeposit2Label(Label):
    pass

class RepeatDepositInput(TextInput):
    pass

class YearsLabel(Label):
    pass

class MonthsLabel(Label):
    pass

class GoalAmount(Label):
    pass

class GoalAmountInput(TextInput):
    def amount(self):
        initial = root.TextInput.text
        goal = int(initial)
        return goal
    pass

#classes for loan screen
class LoanOutputLabel(Label):
    pass

class LoanInstructLabel(Label):
    pass

class LoanAmountLabel(Label):
    pass

class LoanAmountInput(TextInput):
    pass



# Create the screen manager
sm = ScreenManager()
sm.add_widget(SaveScreen(name='save'))
sm.add_widget(LoanScreen(name='loan'))



#class to run app
class InterestApp(App):

    def build(self):
        self.title = "Compound Interest and Loan Application"
        return sm



#run app     
if __name__ == "__main__":
    InterestApp().run()

Main.kv file(mostly used for layout formatting)

<SaveScreen>:
    MainBoxLayout:

        GridLayout:
            cols: 2
            SaveButton:
                on_press: root.manager.current = 'save'
            LoanButton:
                on_press: root.manager.current = 'loan'

        InsideAnchorLayout:
            GridLayout:
                cols: 1
                canvas:
                    Color:
                        rgba: .7,1,.7,1
                    Rectangle:
                        size: self.size
                        pos: self.pos
                SaveInstructLabel:


        InsideAnchorLayout:
            TwoColGridLayout:
                SaveDepositLabel:
                SaveDepositInput:

        InsideAnchorLayout:
            AColGridLayout:

                ChooseCompoundLabel:

        InsideAnchorLayout:
            AColGridLayout:
                cols: 3
                DailyToggle:
                MonthlyToggle:
                YearlyToggle:

        InsideAnchorLayout:
            TwoColGridLayout:
                SaveInterestLabel:
                SaveInterestInput:

        InsideAnchorLayout:
            AColGridLayout:

                RepeatDepositLabel:

        InsideAnchorLayout:
            AColGridLayout:
                cols: 5
                NoneToggle:
                WeeklyToggle:
                BiWeeklyToggle:
                Monthly2Toggle:
                Yearly2Toggle:

        InsideAnchorLayout:
            TwoColGridLayout:
                RepeatDeposit2Label:
                RepeatDepositInput:

        InsideAnchorLayout:
            TwoColGridLayout:
                size_hint_y: None
                height: dp(50)
                SaveYearsLabel:
                TwoColGridLayout:
                    YearsLabel:
                    SaveYearsInput:
                    MonthsLabel:
                    SaveMonthsInput:

        InsideAnchorLayout:
            TwoColGridLayout:
                GoalAmount:
                GoalAmountInput:

        InsideAnchorLayout:
            GridLayout:
                cols: 1
                canvas:
                    Color:
                        rgba: .5,1,1,1
                    Rectangle:
                        size: self.size
                        pos: self.pos
                size_hint: None, None
                height: dp(80)
                width: self.parent.width - dp(15)

                TotalsLabel:
                OutputLabel:
                GoalOutputLabel:
                NoReinvestLabel:

        InsideAnchorLayout:
            AColGridLayout:

                Calculate:






<LoanScreen>:
    MainBoxLayout:

        TwoColGridLayout:
            SaveButton:
                on_press: root.manager.current = 'save'
            LoanButton:
                on_press: root.manager.current = 'loan'

Buttons.kv

<SaveButton>:
    id: 'save'
    text: "[u]Save[/u]"
    color: 1, .9, 0, 1
    background_normal: ''
    background_color: (.5, .5, .5, 1) if self.state == 'normal' else (0, .75, 1, 1)
    group: 'menu'
    markup: True



<LoanButton>:
    text: "[u]Loan[/u]"
    markup: True
    color: 1, .9, 0, 1
    background_normal: ''
    background_color: (.5, .5, .5, 1) if self.state == 'normal' else (0, .75, 1, 1)
    group: 'menu'

<DailyToggle>:
    text: 'Daily'
    group: 'compound'

<MonthlyToggle>:
    text: 'Monthly'
    group: 'compound'

<YearlyToggle>:
    text: 'Yearly'
    group: 'compound'

<WeeklyToggle>:
    text: 'Weekly'
    group: 'repeat'

<Yearly2Toggle>:
    text: 'Yearly'
    group: 'repeat'

<Monthly2Toggle>:
    text: 'Monthly'
    group: 'repeat'

<BiWeeklyToggle>:
    text: 'Bi-Weekly'
    group: 'repeat'

<NoneToggle>:
    text: 'None'
    group: 'repeat'

<Calculate>:
    text: '[u][b]Calculate[/b][/u]'
    markup: True
    on_release: self.add

Labels.kv

<SaveInstructLabel>:
    text: "[b]This is the Savings screen. It will calculate compounded interest over a period of time and tell you the result. Follow each prompt to calculate.[/b]"
    text_size: root.width, None
    size: self.texture_size
    valign: 'middle'
    halign: 'center'
    color: 0,0,0,1
    markup: True

<SaveDepositLabel>:
    text: "Enter initial deposit amount:"
    text_size: root.width, None
    size: self.texture_size
    valign: 'middle'
    halign: 'center'
    color: 0,0,0,1

<ChooseCompoundLabel>:
    text: "Choose frequency of compounding interest:"
    text_size: root.width, None
    size: self.texture_size
    valign: 'middle'
    halign: 'center'
    color: 0,0,0,1

<SaveInterestLabel>:
    text: 'Enter the interest APY:'
    text_size: root.width, None
    size: self.texture_size
    valign: 'middle'
    halign: 'center'
    color: 0,0,0,1

<RepeatDepositLabel>:
    text: 'How often will you make a deposit:'
    text_size: root.width, None
    size: self.texture_size
    valign: 'middle'
    halign: 'center'
    color: 0,0,0,1

<SaveYearsLabel>:
    text: 'Enter the amount of years and months you will have this account build:'
    text_size: root.width, None
    size: self.texture_size
    valign: 'middle'
    halign: 'center'
    color: 0,0,0,1
    font_size: dp(15)

<YearsLabel>:
    text: 'Years:'
    text_size: root.width, None
    size: self.texture_size
    valign: 'middle'
    halign: 'right'
    color: 0,0,0,1

<MonthsLabel>:
    text: 'Months:'
    text_size: root.width, None
    size: self.texture_size
    valign: 'middle'
    halign: 'right'
    color: 0,0,0,1

<GoalAmount>:
    text: '(Optional)Enter an amount you would like to reach:'
    text_size: root.width, None
    size: self.texture_size
    valign: 'middle'
    halign: 'center'
    color: 0,0,0,1

<OutputLabel>:
    text: app.Calculate.add
    text_size: root.width, None
    size: self.texture_size
    valign: 'middle'
    halign: 'center'
    color: 0,0,0,1

<GoalOutputLabel>:
    text: 'total years to reach goal'
    text_size: root.width, None
    size: self.texture_size
    valign: 'middle'
    halign: 'center'
    color: 0,0,0,1

<NoReinvestLabel>:
    text: 'if you didnt reinvest'
    text_size: root.width, None
    size: self.texture_size
    valign: 'middle'
    halign: 'center'
    color: 0,0,0,1

<TotalsLabel>:
    text: '[u][b]TOTALS:[/b][/u]'
    markup: True
    valign: 'middle'
    halign: 'center'
    color: 0,0,0,1

<RepeatDeposit2Label>:
    text: 'Enter recurring deposit amount:'
    text_size: root.width, None
    size: self.texture_size
    valign: 'middle'
    halign: 'center'
    color: 0,0,0,1

Textboxes.kv

<SaveDepositInput>:
    multiline: False
    hint_text: '$0.00'
    input_filter: 'float'
    size_hint_y: None
    height: dp(25)

<SaveInterestInput>:
    multiline: False
    hint_text: '0.0'
    input_filter: 'float'
    size_hint_y: None
    height: dp(25)

<SaveYearsInput>:
    multiline: False
    hint_text: '0'
    input_filter: 'int'
    size_hint_y: None
    height: dp(25)

<SaveMonthsInput>:
    multiline: False
    hint_text: '0'
    input_filter: 'int'
    size_hint_y: None
    height: dp(25)

<GoalAmountInput>:
    multiline: False
    hint_text: '$0.00'
    input_filter: 'float'
    size_hint_y: None
    height: dp(25)

<RepeatDepositInput>:
    multiline: False
    hint_text: '$0.00'
    input_filter: 'float'
    size_hint_y: None
    height: dp(25)

And Layouts.kv

<MainBoxLayout>:
    spacing: dp(2)
    orientation: 'vertical'
    canvas.before:
        Color:
            rgba: .7,1,.7,1   
        Rectangle:
            size: self.size
            pos: self.pos




<TwoColGridLayout>:
    cols: 2
    size_hint: .95, .70
    canvas:
        Color:
            rgba: .5,1,1,1
        Rectangle:
            size: self.size
            pos: self.pos


<OneColGridLayout>:
    cols: 1

<AColGridLayout>:
    cols: 1
    size_hint: .95,.70
    canvas:
        Color:
            rgba: .5,1,1,1
        Rectangle:
            size: self.size
            pos: self.pos



<WidgetRelativeLayout>:



<InsideAnchorLayout>:
    anchor_x: 'center'

Upvotes: 0

Views: 1383

Answers (1)

John Anderson
John Anderson

Reputation: 39137

In your Calculate Button, the add method is trying to call static methods, but the methods you name are actually instance methods. So you need to call them as instance methods. That means that you must have the instances of SaveDepositInput and GoalAmountInput. Since all of these things are in your SaveScreen, you can easily reference them using ids. To do this you cn add an id to SaveDepositInput in your SaveScreen:

    InsideAnchorLayout:
        TwoColGridLayout:
            SaveDepositLabel:
            SaveDepositInput:
                id: save_deposit_input

and for the GoalInputAmount:

    InsideAnchorLayout:
        TwoColGridLayout:
            GoalAmount:
            GoalAmountInput:
                id: goal_amount_input

then, in order to access these from the Calculate Button:

    InsideAnchorLayout:
        AColGridLayout:
            Calculate:
                gai: goal_amount_input
                sdi: save_deposit_input

Then, the CalculateButton class becomes:

class Calculate(Button):
    def add(self):
        total = self.sdi.inideposit() + self.gai.amount()
        return total

A couple other issues. The CalculateButton in your Buttons.kv should be:

<Calculate>:
    text: '[u][b]Calculate[/b][/u]'
    markup: True
    on_release: self.add()

( you were missing the ())

And you have a similar class vs instance issue with your OutputLabel in your Labels.kv.

Upvotes: 1

Related Questions