AttributeError: 'super' object has no attribute '__getattr__' ( I searched, but to no avail)
AttributeError: 'super' object has no attribute '__getattr__' ( I searched, but to no avail)
我正在尝试编写代码来计算粉刷整个房间需要多少油漆。
我的编码:
# main.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from database import DataBase
from kivy.properties import NumericProperty
class CreateAccountWindow(Screen):
panjang = ObjectProperty(None)
lebar = ObjectProperty(None)
tinggi = ObjectProperty(None)
def submit(self):
if self.tinggi.text !="":
try:
if float(self.tinggi.text) and float(self.lebar.text) and float(self.panjang.text):
sm.current = "main"
except:
invalidForm()
else:
invalidForm()
class MainWindow(Screen):
# Each screen has by default a property manager that gives
# you the instance of the ScreenManager used.
# declare class attributes
panjang = ObjectProperty(None)
lebar = ObjectProperty(None)
tingi = ObjectProperty(None)
luas1 = ObjectProperty(None)
luas2 = ObjectProperty(None)
cat = ObjectProperty(None)
def logOut(self):
self.manager.current = "create"
def volume(self):
luas1 = float(self.manager.ids.create.panjang.text) * float(self.manager.ids.create.tinggi.text) / 10
luas2 = float(self.manager.ids.create.lebar.text) * float(self.manager.ids.create.tinggi.text) / 10
self.luas1.text = str(luas1)
self.luas2.text = str(luas2)
self.cat.text = str(luas1 * 2 + luas2 * 2)
def on_enter(self, *args):
self.volume() # calculate volume
self.panjang.text = "Panjang: " + self.manager.ids.create.panjang.text
self.lebar.text = "Lebar: " + self.manager.ids.create.lebar.text
self.tinggi.text = "Tinggi: " + self.manager.ids.create.tinggi.text
self.luas1.text = "Luas:" + self.manager.ids.main.luas1.text
self.luas2.text = "Luas:" + self.manager.ids.main.luas2.text
self.cat.text = "Luas:" + self.manager.ids.main.cat.text
class WindowManager(ScreenManager):
pass
def invalidLogin():
pop = Popup(title='Invalid Login',
content=Label(text='Invalid username or password.'),
size_hint=(None, None), size=(400, 400))
pop.open()
def invalidForm():
pop = Popup(title='Invalid Form',
content=Label(text='Please fill in all inputs with valid information.'),
size_hint=(None, None), size=(400, 400))
pop.open()
kv = Builder.load_file("banyaknyakerja.kv")
sm = ScreenManager()
screens = [CreateAccountWindow(name="create"),MainWindow(name="main")]
for screen in screens:
sm.add_widget(screen)
sm.current = "create"
class MyMainApp(App):
def build(self):
return sm
if __name__ == "__main__":
MyMainApp().run()
KV 文件:
<WindowManager>:
CreateWindow:
id:create
name: "create"
MainWindow:
id: main
name: "main"
<CreateAccountWindow>:
panjang: panjang
lebar: lebar
tinggi: tinggi
FloatLayout:
cols:1
FloatLayout:
size: root.width, root.height/2
Label:
text: "serfbgokmfor"
size_hint: 0.8, 0.2
pos_hint: {"x":0.1, "top":1}
font_size: (root.width**2 + root.height**2) / 14**4
Label:
size_hint: 0.5,0.12
pos_hint: {"x":0, "top":0.8}
text: "Panjang: "
font_size: (root.width**2 + root.height**2) / 14**4
TextInput:
pos_hint: {"x":0.5, "top":0.8}
size_hint: 0.4, 0.12
id: panjang
multiline: False
font_size: (root.width**2 + root.height**2) / 14**4
Label:
size_hint: 0.5,0.12
pos_hint: {"x":0, "top":0.8-0.13}
text: "Lebar: "
font_size: (root.width**2 + root.height**2) / 14**4
TextInput:
pos_hint: {"x":0.5, "top":0.8-0.13}
size_hint: 0.4, 0.12
id: lebar
multiline: False
font_size: (root.width**2 + root.height**2) / 14**4
Label:
size_hint: 0.5,0.12
pos_hint: {"x":0, "top":0.8-0.13*2}
text: "Tinggi:"
font_size: (root.width**2 + root.height**2) / 14**4
TextInput:
pos_hint: {"x":0.5, "top":0.8-0.13*2}
size_hint: 0.4, 0.12
id: tinggi
multiline: False
font_size: (root.width**2 + root.height**2) / 14**4
Button:
pos_hint:{"x":0.3,"y":0.25}
size_hint: 0.4, 0.1
font_size: (root.width**2 + root.height**2) / 17**4
text: "Fefrkglt;mlfavsmdmcfr"
on_release:
root.manager.transition.direction = "left"
root.login()
Button:
pos_hint:{"x":0.2,"y":0.05}
size_hint: 0.6, 0.15
text: "Kira"
font_size: (root.width**2 + root.height**2) / 14**4
on_release:
root.manager.transition.direction = "left"
root.submit()
<MainWindow>:
panjang: panjang
lebar: lebar
tinggi: tinggi
luas1: luas1
luas2: luas2
cat: cat
FloatLayout:
Label:
id: panjang
pos_hint:{"x": 0.1, "top":0.9}
size_hint:0.8, 0.2
text: "Panjang: "
Label:
id: lebar
pos_hint:{"x": 0.1, "top":0.8}
size_hint:0.8, 0.2
text: "Lebar: "
Label:
id: tinggi
pos_hint:{"x": 0.1, "top":0.7}
size_hint:0.8, 0.2
text: "Tinggi:"
Label:
id: luas1
pos_hint:{"x": 0.1, "top":0.6}
size_hint:0.8, 0.2
text: "Luas:"
Label:
id: luas2
pos_hint:{"x": 0.1, "top":0.5}
size_hint:0.8, 0.2
text: "Luas:"
Label:
id: cat
pos_hint:{"x": 0.1, "top":0.4}
size_hint:0.8, 0.2
text: "Cat:"
Button:
pos_hint:{"x":0.2, "y": 0.1}
size_hint:0.6,0.2
text: "Semula"
on_release:
app.root.current = "create"
root.manager.transition.direction = "down"
(它告诉我显示最少的代码量,但该死的我真的不知道这里的问题)
我预期的输出不完整并显示错误消息:
File "C:\Users\dekmeymey\.kivy\banyaknyekerja.py", line 60, in on_enter
self.volume() # calculate volume
File "C:\Users\dekmeymey\.kivy\banyaknyekerja.py", line 52, in volume
luas1 = float(self.manager.ids.create.panjang.text) * float(self.manager.ids.create.tinggi.text) / 10
File "kivy\properties.pyx", line 863, in kivy.properties.ObservableDict.__getattr__
AttributeError: 'super' object has no attribute '__getattr__'
(上面还有很多其他错误信息,但我认为这部分是最重要的。)
我希望它有点像这样:
Panjang: 23
Lebar: 11
Tinggi: 12
luas1: blabla
luas2: blabla
cat yang diperlukan: blabla
你明白了。
(我不会插入图片,所以我就这样做了。)
但是我只得到这个:
Panjang:
Lebar:
Tinggi:
luas1:
luas2:
cat yang diperlukan:
我该如何解决这个问题?
我认为您的代码有几个问题。首先,您正在尝试访问您实际上并未创建的 id
。在您的 kv
行中:
<WindowManager>:
CreateWindow:
id:create
name: "create"
MainWindow:
id: main
name: "main"
将在 WindowManager
实例中创建一些 ids
,但您没有使用 WindowManager
,因此不会创建那些 ids
。另外,上面的 kv
代码会尝试创建一个 CreateWindow
class 的实例,但是没有这样一个 class 的定义。我怀疑你打算使用你的 CreateAccountWindow
,所以我将上面的 kv
代码更改为:
<WindowManager>:
CreateAccountWindow:
id:create
name: "create"
MainWindow:
id: main
name: "main"
接下来,您需要实际使用该 kv
代码。因此,将创建 ScreenManager
的代码更改为:
sm = ScreenManager()
screens = [CreateAccountWindow(name="create"),MainWindow(name="main")]
for screen in screens:
sm.add_widget(screen)
sm.current = "create"
至:
sm = WindowManager()
sm.current = "main"
(注意:我在上面的代码中将 "create" 更改为 "main",只是因为否则我无法弄清楚如何到达 main
Screen
.)
最后,您正在调用 float(self.manager.ids.create.panjang.text)
将文本转换为浮点数,但是如果尚未输入 panjang
的 text
,这将抛出一个 Exception
。因此,您应该确保除非 TextInput
具有有效号码 text
,否则不会调用 float
,或者给它一个默认号码。同样适用于其他 TextInputs
.
我正在尝试编写代码来计算粉刷整个房间需要多少油漆。
我的编码:
# main.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from database import DataBase
from kivy.properties import NumericProperty
class CreateAccountWindow(Screen):
panjang = ObjectProperty(None)
lebar = ObjectProperty(None)
tinggi = ObjectProperty(None)
def submit(self):
if self.tinggi.text !="":
try:
if float(self.tinggi.text) and float(self.lebar.text) and float(self.panjang.text):
sm.current = "main"
except:
invalidForm()
else:
invalidForm()
class MainWindow(Screen):
# Each screen has by default a property manager that gives
# you the instance of the ScreenManager used.
# declare class attributes
panjang = ObjectProperty(None)
lebar = ObjectProperty(None)
tingi = ObjectProperty(None)
luas1 = ObjectProperty(None)
luas2 = ObjectProperty(None)
cat = ObjectProperty(None)
def logOut(self):
self.manager.current = "create"
def volume(self):
luas1 = float(self.manager.ids.create.panjang.text) * float(self.manager.ids.create.tinggi.text) / 10
luas2 = float(self.manager.ids.create.lebar.text) * float(self.manager.ids.create.tinggi.text) / 10
self.luas1.text = str(luas1)
self.luas2.text = str(luas2)
self.cat.text = str(luas1 * 2 + luas2 * 2)
def on_enter(self, *args):
self.volume() # calculate volume
self.panjang.text = "Panjang: " + self.manager.ids.create.panjang.text
self.lebar.text = "Lebar: " + self.manager.ids.create.lebar.text
self.tinggi.text = "Tinggi: " + self.manager.ids.create.tinggi.text
self.luas1.text = "Luas:" + self.manager.ids.main.luas1.text
self.luas2.text = "Luas:" + self.manager.ids.main.luas2.text
self.cat.text = "Luas:" + self.manager.ids.main.cat.text
class WindowManager(ScreenManager):
pass
def invalidLogin():
pop = Popup(title='Invalid Login',
content=Label(text='Invalid username or password.'),
size_hint=(None, None), size=(400, 400))
pop.open()
def invalidForm():
pop = Popup(title='Invalid Form',
content=Label(text='Please fill in all inputs with valid information.'),
size_hint=(None, None), size=(400, 400))
pop.open()
kv = Builder.load_file("banyaknyakerja.kv")
sm = ScreenManager()
screens = [CreateAccountWindow(name="create"),MainWindow(name="main")]
for screen in screens:
sm.add_widget(screen)
sm.current = "create"
class MyMainApp(App):
def build(self):
return sm
if __name__ == "__main__":
MyMainApp().run()
KV 文件:
<WindowManager>:
CreateWindow:
id:create
name: "create"
MainWindow:
id: main
name: "main"
<CreateAccountWindow>:
panjang: panjang
lebar: lebar
tinggi: tinggi
FloatLayout:
cols:1
FloatLayout:
size: root.width, root.height/2
Label:
text: "serfbgokmfor"
size_hint: 0.8, 0.2
pos_hint: {"x":0.1, "top":1}
font_size: (root.width**2 + root.height**2) / 14**4
Label:
size_hint: 0.5,0.12
pos_hint: {"x":0, "top":0.8}
text: "Panjang: "
font_size: (root.width**2 + root.height**2) / 14**4
TextInput:
pos_hint: {"x":0.5, "top":0.8}
size_hint: 0.4, 0.12
id: panjang
multiline: False
font_size: (root.width**2 + root.height**2) / 14**4
Label:
size_hint: 0.5,0.12
pos_hint: {"x":0, "top":0.8-0.13}
text: "Lebar: "
font_size: (root.width**2 + root.height**2) / 14**4
TextInput:
pos_hint: {"x":0.5, "top":0.8-0.13}
size_hint: 0.4, 0.12
id: lebar
multiline: False
font_size: (root.width**2 + root.height**2) / 14**4
Label:
size_hint: 0.5,0.12
pos_hint: {"x":0, "top":0.8-0.13*2}
text: "Tinggi:"
font_size: (root.width**2 + root.height**2) / 14**4
TextInput:
pos_hint: {"x":0.5, "top":0.8-0.13*2}
size_hint: 0.4, 0.12
id: tinggi
multiline: False
font_size: (root.width**2 + root.height**2) / 14**4
Button:
pos_hint:{"x":0.3,"y":0.25}
size_hint: 0.4, 0.1
font_size: (root.width**2 + root.height**2) / 17**4
text: "Fefrkglt;mlfavsmdmcfr"
on_release:
root.manager.transition.direction = "left"
root.login()
Button:
pos_hint:{"x":0.2,"y":0.05}
size_hint: 0.6, 0.15
text: "Kira"
font_size: (root.width**2 + root.height**2) / 14**4
on_release:
root.manager.transition.direction = "left"
root.submit()
<MainWindow>:
panjang: panjang
lebar: lebar
tinggi: tinggi
luas1: luas1
luas2: luas2
cat: cat
FloatLayout:
Label:
id: panjang
pos_hint:{"x": 0.1, "top":0.9}
size_hint:0.8, 0.2
text: "Panjang: "
Label:
id: lebar
pos_hint:{"x": 0.1, "top":0.8}
size_hint:0.8, 0.2
text: "Lebar: "
Label:
id: tinggi
pos_hint:{"x": 0.1, "top":0.7}
size_hint:0.8, 0.2
text: "Tinggi:"
Label:
id: luas1
pos_hint:{"x": 0.1, "top":0.6}
size_hint:0.8, 0.2
text: "Luas:"
Label:
id: luas2
pos_hint:{"x": 0.1, "top":0.5}
size_hint:0.8, 0.2
text: "Luas:"
Label:
id: cat
pos_hint:{"x": 0.1, "top":0.4}
size_hint:0.8, 0.2
text: "Cat:"
Button:
pos_hint:{"x":0.2, "y": 0.1}
size_hint:0.6,0.2
text: "Semula"
on_release:
app.root.current = "create"
root.manager.transition.direction = "down"
(它告诉我显示最少的代码量,但该死的我真的不知道这里的问题)
我预期的输出不完整并显示错误消息:
File "C:\Users\dekmeymey\.kivy\banyaknyekerja.py", line 60, in on_enter
self.volume() # calculate volume
File "C:\Users\dekmeymey\.kivy\banyaknyekerja.py", line 52, in volume
luas1 = float(self.manager.ids.create.panjang.text) * float(self.manager.ids.create.tinggi.text) / 10
File "kivy\properties.pyx", line 863, in kivy.properties.ObservableDict.__getattr__
AttributeError: 'super' object has no attribute '__getattr__'
(上面还有很多其他错误信息,但我认为这部分是最重要的。)
我希望它有点像这样:
Panjang: 23
Lebar: 11
Tinggi: 12
luas1: blabla
luas2: blabla
cat yang diperlukan: blabla
你明白了。
(我不会插入图片,所以我就这样做了。)
但是我只得到这个:
Panjang:
Lebar:
Tinggi:
luas1:
luas2:
cat yang diperlukan:
我该如何解决这个问题?
我认为您的代码有几个问题。首先,您正在尝试访问您实际上并未创建的 id
。在您的 kv
行中:
<WindowManager>:
CreateWindow:
id:create
name: "create"
MainWindow:
id: main
name: "main"
将在 WindowManager
实例中创建一些 ids
,但您没有使用 WindowManager
,因此不会创建那些 ids
。另外,上面的 kv
代码会尝试创建一个 CreateWindow
class 的实例,但是没有这样一个 class 的定义。我怀疑你打算使用你的 CreateAccountWindow
,所以我将上面的 kv
代码更改为:
<WindowManager>:
CreateAccountWindow:
id:create
name: "create"
MainWindow:
id: main
name: "main"
接下来,您需要实际使用该 kv
代码。因此,将创建 ScreenManager
的代码更改为:
sm = ScreenManager()
screens = [CreateAccountWindow(name="create"),MainWindow(name="main")]
for screen in screens:
sm.add_widget(screen)
sm.current = "create"
至:
sm = WindowManager()
sm.current = "main"
(注意:我在上面的代码中将 "create" 更改为 "main",只是因为否则我无法弄清楚如何到达 main
Screen
.)
最后,您正在调用 float(self.manager.ids.create.panjang.text)
将文本转换为浮点数,但是如果尚未输入 panjang
的 text
,这将抛出一个 Exception
。因此,您应该确保除非 TextInput
具有有效号码 text
,否则不会调用 float
,或者给它一个默认号码。同样适用于其他 TextInputs
.