2016-11-07 71 views
0

我正在构建一个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

我仍然无法弄清楚什么我做错了。请帮忙。谢谢。

+0

在Python代码中创建它们时,是否需要使用self.add_widget(button)手动添加小部件?不知道这是你的问题的原因,但现在你只是在内存中创建widget对象,就是这样。实际上没有任何东西添加到屏幕上。 – Synedraacus

回答

1

你的GetTask继承自什么?在我看来,它不会从任何东西继承。尝试改变它

class GetTask(Widget): 
    # The rest is like it's in your code. 

也看看我的问题下的评论。不过,不确定它是否仍然是1.9.1中的问题。

+0

它现在运行,但我得到的只是一个黑屏。这可能是因为没有self.add_widget(按钮)? – user5332025

+0

Nvm。得到它的工作。添加了'GetTask(Widget)'和'self.add_widget(Button)'。非常感谢! – user5332025

相关问题