【问题标题】:Kivy application window does not startKivy 应用程序窗口无法启动
【发布时间】:2021-08-29 20:44:03
【问题描述】:

kivy应用程序窗口不启动,不知道是怎么回事。这是最简单的 Hello World 应用程序,但只是没有出现窗口。 这是main.py中的代码:

from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout

class Container(BoxLayout):
    pass

class FirstApp(App):
    def build(self):
        return Container()
        
if "__name__" == "__main__":
    FirstApp.run()

也在first.kv中:

    Button:
        text: "Hello"

这里是日志:

[INFO   ] [Kivy        ] v2.0.0
[INFO   ] [Kivy        ] Installed at "/usr/local/lib/python3.9/site-packages/kivy/__init__.py"
[INFO   ] [Python      ] v3.9.6 (default, Jun 29 2021, 06:20:32) 
[Clang 12.0.0 (clang-1200.0.32.29)]
[INFO   ] [Python      ] Interpreter at "/usr/local/opt/python@3.9/bin/python3.9"
[INFO   ] [Factory     ] 186 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_imageio, img_dds, img_sdl2, img_pil (img_ffpyplayer ignored)
[INFO   ] [Text        ] Provider: sdl2

这里看起来没有任何错误。 我使用 mac os,Catalina,我还在虚拟机上尝试了并行桌面(在 Ubuntu 和 Windows 上) 对不起我的英语,我不是来自英语国家。 请帮帮我!

【问题讨论】:

  • 你真的有FirstApp.run()吗?那就错了,应该是FirstApp().run()
  • @inclement,是的,我改变了它,但没有任何改变,也许还有其他问题?
  • 发布你正在运行的确切代码,以及你用来运行它的命令。
  • @inclement,这是来自 main.py 的代码:from kivy.app import App from kivy.uix.button import Button from kivy.uix.boxlayout import BoxLayout class Container(BoxLayout): pass class FirstApp(App): def build(self): return Container() if "__name__" == "__main__": FirstApp().run() 这是来自 first.kv 的代码:<Container>: Button: text: "Hello!" Button: text: "World!" 这是命令:python3 main.py 日志是一样的,它们是太大了,无法评论。

标签: python python-3.x kivy


【解决方案1】:

应该是这样的

from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout

class Container(BoxLayout):
    pass

class FirstApp(App):
    def build(self):
        return Container()
# the main reason that your code is not running because you make __name__ as 
# string so this condition will never be true so the app will not load        
if __name__ == "__main__":
    FirstApp().run()

对于kv文件


<Container>:# you miss this line which the root widget
    Button:
        text: "Hello"

【讨论】:

  • 非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多