好吧,我需要获取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”是指奇异果标签
更新:
我认为@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
功能
答案 0 :(得分:0)
解决方案有两个选择。
不建议使用tela_two = Tela2()
,因为您正在创建对象Tela2
的另一个实例,该实例没有与之关联的视图以及属性的不同更新值。
在此选项中,我们使用get_screen()
函数检索实例化的对象Tela2:
,以便我们可以访问其方法或属性,例如price
。我们将进行以下增强:
name: 'Tela2'
和name: 'Tela4'
分别用于实例化对象Tela2:
和Tela4:
。摘要-kv文件
ScreenManager:
Tela2:
name: 'Tela2'
...
Tela4:
name: 'Tela4'
get_screen()
函数检索实例化对象Tela2
摘要-Py文件
def whatever(self):
tela_two = self.manager.get_screen('Tela2')
self.cost_output.text = str(tela_two.price)
或
def whatever(self):
self.cost_output.text = str(self.manager.get_screen('Tela2').price)
在此选项中,我们将id: tela2
添加到实例化对象Tela2:
中,并使用ids.tela2
访问其方法或属性,例如price
。我们将进行以下增强:
id: tela2
添加到实例化对象Tela2:
摘要-kv文件
ScreenManager:
Tela2:
id: tela2
...
Tela4:
tela_two = Tela2
替换为tela_two = self.manager.ids.tela2
摘要-Py文件
def whatever(self):
tela_two = self.manager.ids.tela2
self.cost_output.text = str(tela_two.price)
或
def whatever(self):
self.cost_output.text = str(self.manager.ids.tela2.price)
每个屏幕默认都有一个属性
manager
,该属性为您提供 的ScreenManager
的实例。
以下示例说明了如何使用get_screen()
函数和ids.tela2
访问对象Tela2
中定义的属性。它还通过调用on_text
和self.region_define('Norte')
模拟self.op_dimoff()
事件。
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
from kivy.lang import Builder
Builder.load_string("""
<ScreenManagement>:
Tela2:
id: tela2
name: 'Tela2'
Tela4:
name: 'Tela4'
<Tela2>:
Button:
text: 'Goto Tela4'
on_release: root.manager.current = 'Tela4'
<Tela4>:
on_pre_enter:
self.whatever()
Label:
id: cost_output
font_size: sp(50)
""")
class Tela2(Screen):
ngd = ObjectProperty(None)
def __init__(self, **kwargs):
super(Tela2, self).__init__(**kwargs)
self.price = 0.0
self.ngd = 0.0
self.price_inv = 0.0
# simulation
self.region_define('Norte')
self.op_dimoff()
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)
class Tela4(Screen):
def whatever(self):
# Each screen has by default a roperty manager that gives you the instance of the ScreenManager used.
self.ids.cost_output.text = str(self.manager.ids.tela2.price)
print(f"tela2.irrad={self.manager.ids.tela2.irrad}")
print(f"tela2.kwh={self.manager.get_screen('Tela2').kwh}")
class ScreenManagement(ScreenManager):
pass
class TestApp(App):
def build(self):
return ScreenManagement()
if __name__ == "__main__":
TestApp().run()