【发布时间】:2019-05-24 02:03:19
【问题描述】:
好吧,我需要将 price 变量传递给 Tela 4 函数 whatever。我对 Python 很陌生,我不知道如何做到这一点,即使我认为它很简单
这就是代码现在的样子,请随时添加有关如何改进它的建议。
class Tela2(Screen):
def op_dimoff(self):
self.price = float((int(self.ngd) * 0.87 * 1.75) + self.price_inv)
class Tela4 (Screen):
def whatever(self):
tela_two = Tela2
self.cost_output.text = str(tela_two.price)
“cost_output”指的是 kivy 标签
更新:
我认为@slackmart 的做法是正确的,但我仍然无法得到想要的结果。我收到错误AttributeError: 'Tela2' object has no attribute 'irrad'(对不起,我没有发布更完整的代码版本,这是我在这里的第一个问题,我害怕发布很长的问题)
现在的代码就是这样,如果你们能帮我找到解决方案:
class Tela2(Screen):
ngd = ObjectProperty()
def __init__(self, **kwargs):
super(Tela2, self).__init__(**kwargs)
self.price = 0.0
self.ngd = 0.0
self.price_inv = 0.0
def region_define(self, text):
self.inorte: float = 4.825
self.inordeste: float = 5.483
self.icentro: float = 5.082
self.isudeste: float = 4.951
self.isul: float = 4.444
self.kwh_norte: float = 0.871
self.kwh_nordeste: float = 0.308
self.kwh_centro: float = 0.290
self.kwh_sudeste: float = 0.322
self.kwh_sul: float = 0.320
if text == 'Norte':
self.irrad = self.inorte
self.kwh = self.kwh_norte
elif text == 'Nordeste':
self.irrad = self.inordeste
self.kwh = self.kwh_nordeste
elif text == 'Centro-Oeste':
self.irrad = self.icentro
self.kwh = self.kwh_centro
elif text == 'Sudeste':
self.irrad = self.isudeste
self.kwh = self.kwh_sudeste
else:
self.irrad = self.isul
self.kwh = self.kwh_sul
def op_dimoff(self):
# cálculo da Geração Mínima = (NGD/irrad)
self.gmin = float(self.ngd/self.irrad)# em W/h
# dimensionamento potência do inversor
self.pot_seg = self.gmin * 1.3
self.inv = (600, 1000, 1500, 2000, 3000)
self.pri_inv = (1434, 1852.2, 1924, 2604, 3899)
self.x = 0
for self.x in range(0, len(self.inv)):
if self.pot_seg <= self.inv[int(self.x)]:
self.pot_inv = self.inv[int(self.x)]
self.price_inv = self.pri_inv[int(self.x)]
break
else:
self.x += 1
self.price = float((int(self.ngd) * 0.87 * 1.75) + self.price_inv)
注意事项:
region_define是 kivy 微调器的on_text函数
【问题讨论】:
-
将
tela_two = Tela2更改为tela_two = Tela2() -
@evyllanesc 照你说的做,我得到了这个“AttributeError: 'Tela2' object has no attribute 'price'”
-
使用minimal reproducible example 会更容易回答。例如,Kivy 似乎无关紧要,一堆未定义的名称,例如
Screen和self.ngd,并且您的类已定义但未使用。
标签: python-3.x kivy