我正在构建一个kivy应用程序。下面的代码是一个简单的Hello World类。按按钮。标签的变化,从“你好”到“世界”Kivy应用程序错误 - App.root中的无效实例
import kivy
kivy.require('1.9.1') # replace with your current kivy version !
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
#from tasks import assign_task
class GetTask():
def __init__(self, **kwargs):
super(GetTask,self).__init__(**kwargs)
self.main_label = Label(text = "Hello")
button = Button(text="Press")
button.bind(on_press= self.update)
def update(self):
self.main_label.text = "World"
class MyApp(App):
def build(self):
return GetTask()
if __name__ == '__main__':
MyApp().run()
我得到的错误,当我运行它是:
raise Exception('Invalid instance in App.root')
Exception: Invalid instance in App.root
我看着这个 - Kivy: Invalid instance in App.root
我仍然无法弄清楚什么我做错了。请帮忙。谢谢。
在Python代码中创建它们时,是否需要使用self.add_widget(button)手动添加小部件?不知道这是你的问题的原因,但现在你只是在内存中创建widget对象,就是这样。实际上没有任何东西添加到屏幕上。 – Synedraacus