屏幕管理器(AttributeError:'超级'对象没有属性' __ getattr __')

时间:2018-03-21 00:13:43

标签: python python-3.x kivy kivy-language

我对Kivy编程很新,需要将它用于这个项目。我遇到的问题是,每当我尝试使用屏幕管理器更改屏幕时,我都会收到以下错误。

属性错误:'超级'对象没有属性' getattr '

如果有人能解释如何修复错误,还要解释是什么原因导致的? 任何帮助都非常适合

主要代码:`

mapOf

`

.Kv文件`

  import kivy
import webbrowser
import MSQLHandler
kivy.require('1.10.0')


from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Label
from kivy.uix.widget import Widget
from kivy.uix.listview import ListItemButton
from kivy.properties import ObjectProperty
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, CardTransition

class Login(Screen):

    class kivy_screen_manager(ScreenManager):
        pass

    teacher_connect_image = ObjectProperty()

    username_text_input = ObjectProperty()
    password_text_input = ObjectProperty()

    def LoginButton(self):
        MSQLHandler.LoginSystem(self.username_text_input.text, self.password_text_input.text, 0)

    def changeScreen(self,  next_screen):
        self.manager.current = next_screen


class MainKivyFrameApp(App):

    def build(self):
        return Login()

mainFrame = MainKivyFrameApp()
mainFrame.run()

`

完整错误消息:

 #: import main NewProject
#: import CardTransition kivy.uix.screenmanager.CardTransition

ScreenManager:
    Login:
        id: kivy_screen_manager
        name: "Login"
    LogedInScreen:
        id: LogedInScreen
        name: "LogedInScreen"



<Login@Screen>:
    id: kivy_screen_manager
    name: "Login"
    teacher_connect_image: logo_image
    username_text_input: username
    password_text_input: password
    teahcerId_text_input: teacherId
    name: "LoginMain"

    BoxLayout:
        padding: 10
        spacing: 10
        orientation: "vertical"
        Image:
            source: 'ApplicationMainLogo.png'
            id: logo_image

        Label:
            text: "Teacher ID"
        TextInput:
            font_size: root.height / 20
            id: teacherId

        Label:
            text: "Username"
        TextInput:
            font_size: root.height / 20
            id: username

        Label:
            text: "Password"
        TextInput:
            font_size: root.height / 20
            id: password

        Button:
            text: "Login"
            on_release: app.root.changeScreen("LogedInScreen")




<LogedInScreen@Screen>:
    id: LogedInScreen
    name: "LogedInScreen"
    padding: 10
    spacing: 10
    orientation: "vertical"

    Button:
        text: "Back Home?"

&#39;

1 个答案:

答案 0 :(得分:0)

错误消息准确无误:

Traceback (most recent call last):
   File "kivy\properties.pyx", line 836, in kivy.properties.ObservableDict.__getattr__ (kivy\properties.c:12509)
 KeyError: 'kivy_screen_manager'

表示kivy_screen_manager不属于ids, ids存储在字典中。

只有登录的子项可以通过id访问,如果我们在摘要中看到.kv,我们会有以下内容,并且kivy_screen_manager显然不在登录状态。

<Login@Screen>:
    ...
    name: "LoginMain"

    BoxLayout:
        ...
        Label:
            ...
        TextInput:
            ...
            id: teacherId
        Label:
            ...
        TextInput:
            ...
            id: username
        Label:
            ...
        TextInput:
            ...
            id: password
        Button:
            ...

如果您想从屏幕访问ScreenManager,则必须使用manager属性,在您的情况下,它会更改:

self.ids.kivy_screen_manager.current = "LogedInScreen" 

self.manager.current = "LogedInScreen"

<强>更新

问题是root是Login,而不是ScreenManager,而root之上没有任何东西,所以ScreenManager即使你声明了它也不存在。

假设.kv文件名为design.kv,则必须修改以下部分:

<强> design.kv

....
<Login@Screen>:
    ...
        Button:
            text: "Login"
            on_release: root.changeScreen("LogedInScreen")

...

并使用Builder加载.kv:

from kivy.lang import Builder

....

class MainKivyFrameApp(App):
    def build(self):
        root = Builder.load_file("design.kv")
        return root

...
相关问题