Kivy:TypeError:NoneType对象不可用于MainApp()。run()

时间:2020-08-28 23:34:06

标签: python python-3.x kivy

我正在尝试使用Python + Kivy开发一个应用程序。每当我尝试在PyCharm中运行它时,都会说DigitalLove().run()出现错误TypeError: 'NoneType' object is not callable。完整的日志如下:

[INFO   ] [Logger      ] Record log in C:\Users\paisseon\.kivy\logs\kivy_20-08-28_14.txt
[INFO   ] [deps        ] Successfully imported "kivy_deps.gstreamer" 0.2.0
[INFO   ] [deps        ] Successfully imported "kivy_deps.angle" 0.2.0
[INFO   ] [deps        ] Successfully imported "kivy_deps.glew" 0.2.0
[INFO   ] [deps        ] Successfully imported "kivy_deps.sdl2" 0.2.0
[INFO   ] [Kivy        ] v2.0.0rc3, git-20c14b2, 20200615
[INFO   ] [Kivy        ] Installed at "C:\Users\paisseon\AppData\Roaming\Python\Python38\site-packages\kivy\__init__.py"
[INFO   ] [Python      ] v3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:43:08) [MSC v.1926 32 bit (Intel)]
[INFO   ] [Python      ] Interpreter at "C:\Users\paisseon\AppData\Local\Programs\Python\Python38-32\python.exe"
[INFO   ] [Factory     ] 185 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_pil (img_sdl2, img_ffpyplayer, img_gif ignored)
 Traceback (most recent call last):
   File "C:/Users/paisseon/Downloads/DigitalLove/main.py", line 846, in <module>
     DigitalLove().run()
   File "C:\Users\paisseon\AppData\Roaming\Python\Python38\site-packages\kivy\app.py", line 949, in run
     self._run_prepare()
   File "C:\Users\paisseon\AppData\Roaming\Python\Python38\site-packages\kivy\app.py", line 919, in _run_prepare
     root = self.build()
 TypeError: 'NoneType' object is not callable

我刚刚开始学习Kivy,因此,如果这是初学者的错误,请原谅我,但是我无法通过在此处搜索或Google来找到解决方案。

预先感谢您:)

编辑: 可重现的示例最少

main.py

from kivy.app import App
from kivy.lang import Builder

GUI = Builder.load_file('main.kv')


class DigitalLove(App):
    @property
    def build(self):

        return GUI

        monika_art = 'images/monika.png'
        textbox_img = 'images/textbox.png'

        doki_response = 'example text'


if __name__ == "__main__":
    DigitalLove().run()

main.kv:

<GUI>:
    canvas:
        Rectangle:
            pos: self.pos
            size: self.size
            source: root.monika_art

            TextInput:
                id: user_input
                text: root.textinput_text
                multiline: False
                font_size: '12sp'
                # Add pos_hint and size_hint

            GridLayout:
                rows: 1
                Image:
                    source: root.textbox_img
                    pos: self.parent.pos
                    size: self.parent.size
                # Add pos_hint and size_hint
                    Label:
                        text: root.doki_response
                        font_size: '16sp'
                        center: self.parent.center

1 个答案:

答案 0 :(得分:0)

这里有几个问题:

  • 您不应将@property装饰器用于build()方法
  • 如果您尚未定义GUI类,请在kv中引用GUI
  • 您在root.中引用了多个kv属性(将引用GUI类中的属性),但未定义。
  • return方法中,build()之后有一些代码。 return之后的任何代码将永远不会执行。
  • 在您的kv中,您的缩进使TexInput的{​​{1}}和GrdLayout成为子代,但这是行不通的。
  • 在您的canvas中,最顶层的规则是kv,它创建了创建<GUI>:类实例时要遵循的规则,但是您没有代码来创建这样的实例。您可以通过将GUI更改为<GUI>:来解决此问题,甚至更好的是,将类名更改为GUI:

这是您的代码的修改后的版本,其中进行了上述更正:

main.py:

Gui

main.kv:

from kivy.app import App
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.uix.boxlayout import BoxLayout


class Gui(BoxLayout):
    monika_art = StringProperty('images/monika.png')
    textbox_img = StringProperty('images/textbox.png')
    doki_response = StringProperty('example text')
    textinput_text = StringProperty('textinput text')

GUI = Builder.load_file('main.kv')


class DigitalLove(App):
    def build(self):
        return GUI


if __name__ == "__main__":
    DigitalLove().run()

Gui: canvas: Rectangle: pos: self.pos size: self.size source: root.monika_art TextInput: id: user_input text: root.textinput_text multiline: False font_size: '12sp' # Add pos_hint and size_hint GridLayout: rows: 1 Image: source: root.textbox_img pos: self.parent.pos size: self.parent.size # Add pos_hint and size_hint Label: text: root.doki_response font_size: '16sp' center: self.parent.center 中没有kv的顶级规则将成为根窗口小部件(<>文件中仅允许一个规则)。加载该kv文件后,将创建根窗口小部件(如果定义了根窗口小部件)并返回。

同样,创建一个与类(kv)同名的变量(GUI)是一个坏主意。它可能有用,但很危险。

相关问题