Python 第一次尝试运行 KIVY

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19113270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:54:07  来源:igfitidea点击:

Trying to run KIVY, for the first time

pythonkivy

提问by user2834165

I'm trying to run kivy for the first time. Im using a default program.

我第一次尝试运行 kivy。我使用默认程序。

from kivy.app import App
from kivy.uix.widget import Widget


class PongGame(Widget):
    pass


class PongApp(App):
    def build(self):
        return PongGame()


if __name__ == '__main__':
    PongApp().run()

I get this error:

我收到此错误:

##################################
done bootstraping kivy...have fun!\n
running "python.exe C:\Python27\hello.py" \n
Traceback (most recent call last):
  File "C:\Python27\hello.py", line 1, in <module>
    from kivy.app import App
ImportError: No module named kivy.app
Press any key to continue . . .

A lot of people have raised the issue online, but no one has mentioned the right solution.

很多人在网上提出了这个问题,但没有人提到正确的解决方案。

采纳答案by Erik Kaplun

UPDATE: based on the error you're getting—which you just pasted now, after my original response below—, you seem to be missing not only PyGame but Kivy itself. Go ahead and run pip install kivy.

更新:根据你得到的错误——你现在刚刚粘贴,在我下面的原始回复之后——,你似乎不仅缺少 PyGame,还缺少 Kivy 本身。快跑吧pip install kivy

But before you do that, I'd recommend you take a look at virtualenvand install all your Python packages specific to this project in a virtualenv created for that project. If you don't want that, you have to run sudo pip install kivyto install Kivy globally (assuming you're on OS X or Linux). On Windows, sudoshould not be needed.

但在你这样做之前,我建议你看看virtualenv并在为该项目创建的virtualenv 中安装特定于该项目的所有 Python 包。如果您不希望那样,您必须运行sudo pip install kivy以全局安装 Kivy(假设您使用的是 OS X 或 Linux)。在 Windows 上,sudo应该不需要。

(Also, I'm sure the information below will be useful as well—since you don't even have Kivy, it must mean that you would have run into problems for not having PyGame either once would have installed Kivy.)

(另外,我相信下面的信息也会有用——因为你甚至没有 Kivy,这一定意味着你会遇到问题,因为如果没有 PyGame 或者一旦安装了 Kivy。)

ORIGINAL ANSWER:

原始答案:

Short version:

精简版:

You're missing PyGame, which is a dependency of Kivy.

您缺少 PyGame,它是 Kivy 的一个依赖项。

Long version:

长版:

Since you didn't tell us what the error was, I went ahead and ran your code on my OS X 10.8 machine and got this:

由于你没有告诉我们错误是什么,我继续在我的 OS X 10.8 机器上运行你的代码并得到这个:

$ python main.py
[INFO   ] Kivy v1.7.2
...
[CRITICAL] [Window      ] Unable to find any valuable Window provider at all!
[CRITICAL] [App         ] Unable to get a Window, abort.

googling that error landed me on http://kivy.org/docs/installation/troubleshooting-macosx.html.

谷歌搜索这个错误让我登陆了http://kivy.org/docs/installation/troubleshooting-macosx.html

So I went ahead and installed PyGame with the help of http://juliaelman.com/blog/2013/04/02/installing-pygame-on-osx-mountain-lion/; except I installed it in a virtualenv:

所以我继续在http://juliaelman.com/blog/2013/04/02/installing-pygame-on-osx-mountain-lion/的帮助下安装了 PyGame ;除了我将它安装在 virtualenv 中:

$ pip install hg+http://bitbucket.org/pygame/pygame

after that:

在那之后:

$ python yourcode.py
[INFO   ] Kivy v1.7.2
Purge log fired. Analysing...
Purge finished !
[INFO   ] [Logger      ] Record log in /Users/erik.allik/.kivy/logs/kivy_13-10-01_2.txt
[INFO   ] [Factory     ] 144 symbols loaded
[DEBUG  ] [Cache       ] register <kv.lang> with limit=None, timeout=Nones
[DEBUG  ] [Cache       ] register <kv.image> with limit=None, timeout=60s
...
[INFO   ] [OSC         ] using <multiprocessing> for socket
[DEBUG  ] [Base        ] Create provider from mouse
[INFO   ] [Base        ] Start application main loop

And I get a nice Kivy window popping up!

我弹出了一个漂亮的 Kivy 窗口!

回答by Adrián Neira

Maybe it's much more simple, it was for me. On kivy webpage provide a ‘portable package' for windows OS so you don't have to install anything “system” wide.

也许它更简单,它是给我的。在 kivy 网页上为 Windows 操作系统提供了一个“便携包”,这样你就不必在“系统”范围内安装任何东西。

Then they explain how to make a shortcut to this ‘portable package' on the Send-to menu. I've pasted the .bat instead of it's shortcut (never said i was smart) and I had (obviously) exacly the same error. If we can call it an error.

然后他们解释了如何在“发送到”菜单上为这个“便携包”创建快捷方式。我已经粘贴了 .bat 而不是它的快捷方式(从来没有说过我很聪明)并且我(显然)有同样的错误。如果我们可以称之为错误。

Hope it works for you too.

希望它也适用于你。