首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Kivy: App.root中的无效实例

Kivy: App.root中的无效实例
EN

Stack Overflow用户
提问于 2014-06-20 19:28:06
回答 2查看 12.5K关注 0票数 3

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

代码语言:javascript
复制
[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

代码文件结构如下:

代码如下: File:main.py

代码语言:javascript
复制
from Src.AppStart import MyClass

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

File:AppStart.py:

代码语言:javascript
复制
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

File:LoginForm.py:

代码语言:javascript
复制
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是一个无效的实例,

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-06-20 19:34:40

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

票数 9
EN

Stack Overflow用户

发布于 2021-07-19 00:29:45

代码语言:javascript
复制
#The wrong just you are forget to add brackets to the end #of class it's want to return.
#Ex:
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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24334362

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档