【问题标题】:Kivy: Invalid instance in App.rootKivy:App.root 中的无效实例
【发布时间】:2014-06-20 19:28:06
【问题描述】:

我是 Python 和 Kivy 的新手,这是我的第一个小项目,不知道我做错了什么,以下是来自 pydev(eclipse) 的日志:

[INFO              ] Kivy v1.8.0
[INFO              ] [Logger      ] Record log in C:\Users\Sudheer\.kivy\logs\kivy_14-06-21_10.txt
[INFO              ] [Factory     ] 157 symbols loaded
[DEBUG             ] [Cache       ] register <kv.lang> with limit=None, timeout=Nones
[DEBUG             ] [Cache       ] register <kv.image> with limit=None, timeout=60s
[DEBUG             ] [Cache       ] register <kv.atlas> with limit=None, timeout=Nones
[INFO              ] [Image       ] Providers: img_tex, img_dds, img_pygame, img_gif (img_pil ignored)
[DEBUG             ] [Cache       ] register <kv.texture> with limit=1000, timeout=60s
[DEBUG             ] [Cache       ] register <kv.shader> with limit=1000, timeout=3600s
[INFO              ] [Text        ] Provider: pygame
[DEBUG             ] [Cache       ] register <kv.loader> with limit=500, timeout=60s
[INFO              ] [Loader      ] using a thread pool of 2 workers
[DEBUG             ] [Cache       ] register <textinput.label> with limit=None, timeout=60.0s
[DEBUG             ] [Cache       ] register <textinput.width> with limit=None, timeout=60.0s
[DEBUG             ] [App         ] Loading kv <D:\OS Files\workspace\Kal\Src\myclass.kv>
[DEBUG             ] [App         ] kv <D:\OS Files\workspace\Kal\Src\myclass.kv> not found
[CRITICAL          ] App.root must be an _instance_ of Widget
 Traceback (most recent call last):
   File "D:\OS Files\workspace\Kal\__main__.py", line 9, in <module>
     MyClass().run()    
   File "C:\Kivy180\kivy\kivy\app.py", line 772, in run
     raise Exception('Invalid instance in App.root')
 Exception: Invalid instance in App.root

代码文件结构如下:

代码如下: 文件:ma​​in.py

from Src.AppStart import MyClass

if __name__ == '__main__':
    MyClass().run()    

文件:AppStart.py:

from kivy.app import App
from Src.Logins.LoginForm import LoginForm
from kivy.uix.button import Button

class MyClass(App):
    '''
    classdocs
    '''
    def build(self):
        c=LoginForm
        #c=Button(text="Checked")
        return c

文件:LoginForm.py:

from kivy.uix.gridlayout import GridLayout
from kivy.graphics import Color
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput

class LoginForm(GridLayout):
    '''
    classdocs
    '''
    def __init__(self, **kwargs):
        #kwargs['cols'] = 1

        #Layout=GridLayout(cols=2, rows=3, background_color=Color(1,1,1))
        self.cols=2
        self.rows=3
        self.background_color=Color(1,1,1)

        IDlbl =Label(text="User ID: ")
        PWlbl =Label(text="Password: ")
        IDtxtbox = TextInput(text="",multiline=False)
        PWtxtbox = TextInput(text="",multiline=False, password=True)

        self.add_widget(IDlbl)
        self.add_widget(PWlbl)
        self.add_widget(IDtxtbox)
        self.add_widget(PWtxtbox)
        #return Layout
        super(LoginForm, self).__init__(**kwargs)

能否请您告诉我为什么 App.root 是无效实例,

【问题讨论】:

    标签: python python-3.x pydev kivy


    【解决方案1】:

    App.build() 的返回值被赋值为App.root。在您的 build() 中,您返回一个类 (LoginForm) 而不是该类的实例。只需将 build() 中的那一行更改为 c = LoginForm() 即可修复它。

    【讨论】:

    • 哇,真快,谢谢。所以 with () 就像处理构建,我会记住这一点。谢谢你。但是,现在我面临一个新错误 :),“AttributeError: 'LoginForm' object has no attribute '_trigger_layout'”,请您帮帮我,否则我需要发布一个新问题。代码中的缩进是正确的。
    • @surpavan 将super(LoginForm, self).__init__(**kwargs) 移动到__init__ 的顶部。超类构造函数创建_trigger_layout 属性。作为一般规则,始终首先调用超类构造函数,除非您在超类初始化之前有特殊需要做某事(例如修改kwargs)。
    • @RyanP:对这里的人有很大帮助.. 谢谢!!
    【解决方案2】:

    你忘了在课尾加括号:

    from kivy.app import App
    from kivy.uix.label import Label
    from kivy.uix.button import Button
    from kivy.uix.gridlayout import  GridLayout
    from kivy.uix.textinput import TextInput 
    from kivy.uix.widget import Widget
    
    
    class MyGridLayout(GridLayout,Widget):
        pass
    
    
    class myapp(App):
        def build(self):
            return MyGridLayout()#  <==== exactly here####
    
    if __name__=="__main__" :
        myapp().run()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-09
      • 2016-02-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多