AttributeError:“超级”对象没有属性“ __getattr __”(我进行了搜索,但无济于事)

时间:2019-07-13 19:43:39

标签: python kivy attributeerror getattr getattribute

我正在尝试编写代码来计算整个房间的油漆量。

我的编码:

# 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:

我该如何解决?

1 个答案:

答案 0 :(得分:0)

我认为您的代码有几个问题。首先,您尝试访问的是您实际上并未创建的id。在您的kv行中:

<WindowManager>:
    CreateWindow:
        id:create
        name: "create"

    MainWindow:
        id: main
        name: "main"

将在ids实例中创建一些WindowManager,但是您没有使用WindowManager,因此不会创建那些ids。另外,上面的kv代码将尝试创建CreateWindow类的实例,但是没有此类的定义。我怀疑您打算使用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"

(注意:在上面的代码中,我将“创建”更改为“主”,只是因为否则我无法弄清楚如何进入main Screen。)

最后,您正在调用float(self.manager.ids.create.panjang.text)将文本转换为浮点数,但是如果尚未输入text的{​​{1}},则会抛出{{1} }。因此,您应该确保不会发生对panjang的调用,除非Exception的有效号码为float,或者给它提供默认号码。其他TextInput也是如此。