【问题标题】:Kivy App Error- Invalid instance in App.rootKivy App 错误 - App.root 中的实例无效
【发布时间】: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) 手动添加小部件吗?不确定这是您问题的原因,但现在您只是在内存中创建小部件对象,仅此而已。什么都没有真正添加到屏幕上。

标签: android python kivy


【解决方案1】:

您的 GetTask 继承自什么?在我看来,它根本没有继承任何东西。尝试将其更改为

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

还可以查看我在问题下的评论。不过,不确定这是否仍然是 1.9.1 中的问题。

【讨论】:

  • 它现在可以运行,但我得到的只是黑屏。这可能是因为没有 self.add_widget(button) 吗?
  • Nvm。让它工作。添加了class GetTask (Widget)self.add_widget(Button)。非常感谢朋友!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-15
  • 1970-01-01
  • 2011-09-08
  • 2018-11-03
  • 2021-03-09
  • 1970-01-01
相关资源
最近更新 更多