python 从def里面调用另一个def函数内容_在python中从一个类调用def到另一个类

本文探讨了如何在Python中从一个类的方法内部调用另一个类的函数。作者遇到的问题是在尝试从'def on_text()'调用'def calculate()'时遇到了AttributeError。示例代码显示了一个Kivy应用的布局,其中包含了两个类及其相关方法的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我有两个文件demo.py和demo.kv。

有人能告诉我如何从一个类调用函数到另一个类吗?我想从def on_text(self, text_input, value):调用def calculate(self):,现在我正在使用代码def on_text(self, text_input, value):

App.get_running_app().User.calculate()

但它给出错误AttributeError:“Test”对象没有属性“User”

在py演示在

^{pr2}$

在演示电压在:

size_hint_y: None

height: self.minimum_height

height: 40

Button:

text: root.button_text

size_hint_x: None

top: 200

TextInput:

text: root.col_data3

width: 300

TextInput:

id: number_input

text: root.col_data4

width: 300

input_filter: 'int'

:

size_hint_y: None

height: self.minimum_height

orientation: "vertical"

:

id: user

total_value:total_value

BoxLayout:

orientation: "vertical"

padding : 20, 5

BoxLayout:

orientation: "horizontal"

#padding : 10, 10

spacing: 10, 10

size: 450, 40

size_hint: None, None

Label:

size_hint_x: .2

text: "Number"

text_size: self.size

valign: 'bottom'

halign: 'center'

Label:

size_hint_x: .4

text: "name"

text_size: self.size

valign: 'bottom'

halign: 'center'

Label:

size_hint_x: .4

text: "Value"

text_size: self.size

valign: 'bottom'

halign: 'center'

ScrollView:

Rows:

id: rows

BoxLayout:

orientation: "horizontal"

padding : 10, 5

spacing: 10, 10

size: 200, 40

size_hint: None, None

Label:

size_hint_x: .7

text: "Total value"

TextInput:

id: total_value

on_focus:root.test()

BoxLayout:

orientation: "horizontal"

size_hint_x: .2

size_hint_y: .2

Button:

text: "+Add More"

on_press: root.add_more()

:

BoxLayout:

orientation: "vertical"

Button:

size_hint: .25, .03

text: "+Add"

on_press: root.add()

GridLayout:

size_hint: 1, None

size_hint_y: None

height: 25

cols: 3

BoxLayout:

orientation: "vertical"

:

text_size: self.size

valign: "middle"

padding_x: 5

size : (100, 40)

size_hint : (None, None)

background_color: 90 , 90, 90, 90

background_normal: ''

color: 0, 0.517, 0.705, 1

border: (0, 10, 0, 0)

:

content_area: content_area

BoxLayout:

orientation: 'vertical'

spacing : 10

BoxLayout:

canvas.before:

Rectangle:

pos: self.pos

size: self.size

size_hint_y: 2

MenuButton:

text: 'Menu'

size : (50, 12)

on_release: root.display()

BoxLayout:

id: content_area

size_hint_y: 30

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值