我为 Kivymd 程序编写了这段代码,用于求解数学方程。我没有添加求解对象,因为它们太长了,但是,我会尽力使问题尽可能清晰
这是代码:
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.core.window import Window
from kivymd.uix.screen import MDScreen
from kivy.properties import DictProperty
from kivymd.uix.screenmanager import MDScreenManager
__version__ = "1.0.0"
kv = """WindowManager:
Home:
Heugebra:
<Home>:
name: "Home"
FloatLayout:
orientation: "vertical"
MDCard:
size: 500,400
FloatLayout:
size: 500,400
MDTextField:
id: Equ
hint_text: "Insert the Equation"
size_hint: None,None
width: 200
pos_hint: {"x": 0.3,"y": 0.3}
on_text_validate: root.get_data()
MDLabel:
id: Eq_anser
text: "test"
pos_hint: {"x": 0.4,"y": 0.3}
MDRoundFlatButton:
text: "Cal"
pos_hint: {"x": 0.8,"y": 0.3}
on_press: app.get_data()
MDRoundFlatButton:
text: "Expand"
pos_hint: {"X": 0.4, "y": 0.3}
on_press: app.expand()
MDRoundFlatButton:
text: "Draw"
pos_hint: {"x": 0.4,"y": 0.25}
on_release:
app.root.current = "Heugebra"
root.manager.transition.direction = "left"
MDFloatingActionButtonSpeedDial:
data: app.data
root_button_anim: True
<Heugebra>:
name: "Heugebra"
MDRoundFlatButton:
text: "Back to Home"
pos_hint: {"x": 0.3, "Y": 0.5}
on_release:
app.root.current = "Home"
root.manager.transition.direction = "right"
"""
class WindowManager(MDScreenManager):
pass
class Home(MDScreen):
pass
class Heugebra(MDScreen):
pass
class MAIN(MDApp):
data = DictProperty()
def on_start(self):
Window.set_title('Every Solver')
def build(self):
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "Indigo"
self.data = {
"Integrate": ["math-integral",
"on_press", lambda x: self.calculas('int')],
"Diff": ["math-diff.png",
"on_press", lambda x: self.calculas('diff')],
}
return Builder.load_string(kv)
def calculas(self, calc: str):
print(calc)
def get_data(self):
x = self.root.ids.Equ.text
self.root.ids.Eq_anser.text = "None"
def expand(self):
x = self.root.ids.Equ.text
self.root.ids.Eq_anser.text = "None"
if __name__ == '__main__':
MAIN().run()
问题是当我尝试单击“Sol”或“Expand”按钮时,会出现此错误: AttributeError:“超级”对象没有属性“getattr”。您的意思是:“setattr”吗?
问题是
self.root
(ScreenManager
)引用的小部件没有Equ或Eq_anser ids。
请注意,ids 可在定义它们的规则内访问,包含这些 ids 的字典是
Home
实例 ids
字典,这是定义它们的规则:
<Home>:
name: "Home"
FloatLayout:
MDCard:
FloatLayout:
MDTextField:
id: Equ
MDLabel:
id: Eq_anser
因此,要访问它们,您需要引用
Home
实例,例如:
def get_data(self):
x = self.root.get_screen("Home").ids.Equ.text.
self.root.get_screen("Home").ids.Eq_anser.text = "None".
def expand(self):
x = self.root.get_screen("Home").ids.Equ.text
self.root.get_screen("Home").ids.Eq_anser.text = "None"