Kivy: App.root 中的无效实例
我刚开始学习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
代码文件的结构如下:
代码如下: 文件:main.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是一个无效的实例吗?
2 个回答
1
在编程中,有时候我们会遇到一些问题,尤其是在使用某些工具或库的时候。这些问题可能会让我们感到困惑,不知道该怎么解决。比如,有人可能在使用某个特定的功能时,发现它没有按照预期工作,或者出现了错误信息。这种情况下,通常我们需要仔细检查代码,看看哪里出了问题。
有时候,解决问题的方法可能很简单,只是需要调整一些设置,或者更新到最新版本的工具。其他时候,可能需要查阅文档,了解这个功能的具体用法,或者在网上寻找类似的问题和解决方案。
总之,遇到问题时,不要慌张,先冷静下来,逐步分析,通常都能找到解决办法。
#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()
9
App.build()
的返回值被赋值给 App.root
。在你的 build()
方法中,你返回的是一个类(LoginForm
),而不是这个类的一个实例。只需要把 build()
中那一行改成 c = LoginForm()
就可以解决这个问题。