如何在kivy中引用其他类方法

2024-09-27 21:24:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我对栈溢出还不熟悉,但我已经用python编程好几年了。有一件事我没有做太多的是面向对象编程。我刚开始学习kivy,我想我可能把我的程序的根搞砸了。我使用类来定义每个标签、按钮、布局…等等。现在我想知道如何引用每个文本输入的属性来使用我要定义的其他方法所填充的内容。你知道吗

示例:按下have按钮从两个不同的文本输入中获取输入,并将它们添加到一起,然后一个标签显示它。你知道吗

你可以看到我在代码中尝试了什么,但是我花了很长时间才弄明白如何从不同的类中引用所有这些东西。你知道吗

我希望我的代码是可以理解的…对不起,它很长,但我希望你看到我所做的一切范围。我真的很希望我能得到一个解决方案,我可以保持我的组织和某些东西只显示在自己的文件,但我明白如果我必须改变很多东西。。。你知道吗

你知道吗主.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()

你知道吗主电源.kv文件(主要用于布局格式)

<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'

你知道吗100.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

你知道吗标签.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

你知道吗文本框.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)

以及1.5千伏你知道吗

<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'

Tags: textfromimportselfnonesizepassroot
1条回答
网友
1楼 · 发布于 2024-09-27 21:24:41

Calculate按钮中,add方法试图调用静态方法,但您命名的方法实际上是实例方法。所以需要将它们作为实例方法调用。这意味着您必须有SaveDepositInputGoalAmountInput的实例。因为所有这些东西都在SaveScreen中,所以可以使用ids轻松地引用它们。为此,您可以在SaveScreen中的id中添加一个SaveDepositInput

    InsideAnchorLayout:
        TwoColGridLayout:
            SaveDepositLabel:
            SaveDepositInput:
                id: save_deposit_input

对于GoalInputAmount

    InsideAnchorLayout:
        TwoColGridLayout:
            GoalAmount:
            GoalAmountInput:
                id: goal_amount_input

然后,为了从Calculate按钮访问这些内容:

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

然后,CalculateButton类变成:

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

还有几个问题。在Buttons.kv中的CalculateButton应该是:

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

(您缺少()

您的Labels.kv中的OutputLabel也有类似的类与实例问题。你知道吗

相关问题 更多 >

    热门问题