【发布时间】:2016-11-07 04:07:46
【问题描述】:
我正在构建一个 kivy 应用程序。下面的代码是一个简单的Hello World。按按钮。标签从“Hello”变为“World”
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) 手动添加小部件吗?不确定这是您问题的原因,但现在您只是在内存中创建小部件对象,仅此而已。什么都没有真正添加到屏幕上。