如何在 Pycharm 中使用 Kivy (1.10.1) 解释器(社区版 2018.3.5)
How to Use Kivy (1.10.1) Interpreter in Pycharm (Community Edition 2018.3.5)
我想开始学习Kivy。我一直在使用 Pycharm 作为我的 IDE 进行 Python 编程。如何将默认的 python 解释器更改为 kivy 解释器以便 pycharm 可以识别 kivy 代码?
我已经安装了 kivy.app 并创建了符号链接。我还使用 pip 安装了 kivy。在我的 python 程序中,我已经能够从 kivy.app 成功导入应用程序并且它可以工作。但是当我编写代码来设计一个小部件(在本例中为 Box Layout)时,Pycharm 带有红色下划线并且不执行代码。
以下代码工作正常:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class Widget1(BoxLayout):
pass
class MyApp(App):
def build(self):
return Widget1()
if __name__ == "__main__":
MyApp().run()
但是当我编写下面的代码来设计Box Layout时,它不起作用。 Pycharm下划线:Widget1、Button、text和Label(写错是:Unresolved Reference)
<Widget1>
Button:
text: "Please click here"
Label:
text: "Button has not been clicked yet"
如果所有代码都有效,在 运行ning 之后,它应该 return 将一个 Box Layout 一分为二。一部分是一个可点击的按钮,上面写着 "Please click here",另一部分只是一个标签,上面写着 "Button has not been clicked yet"。但是现在当我 运行 时,它只是 return 一个空的框布局(没有标签,没有按钮,没有文本)。
问题 2
the Box Layout code (<Widget1>
) still doesn't work.
解决方案
1) kv 文件名
由于您没有使用 Kivy Builder 来加载您的 kv codes/file,因此您正在按照名称约定加载 kv codes/file。确保你的 kv 文件名是 my.kv
There are two ways to load Kv code into your application:
By name convention:
Kivy looks for a Kv file with the same name as your App class in
lowercase, minus “App” if it ends with ‘App’ e.g:
MyApp -> my.kv
If this file defines a Root Widget it will be attached to the App’s
root attribute and used as the base of the application widget tree.
By Builder convention: You can tell Kivy to directly load a string or a file. If this string or file defines a root widget, it
will be returned by the method:
Builder.load_file('path/to/file.kv')
or:
Builder.load_string(kv_string)
2) kv 规则上下文
在您的 kv 文件中,在 class 规则后添加 :
(全冒号),<Widget1>
片段
<Widget1>:
A Kv source constitutes of rules, which are used to describe the
content of a Widget, you can have one root rule, and any number of
class or template rules.
The root rule is declared by declaring the class of your root
widget, without any indentation, followed by : and will be set as the
root attribute of the App instance:
Widget:
A class rule, declared by the name of a widget class between < >
and followed by :, defines how any instance of that class will be
graphically represented:
<MyWidget>:
Rules use indentation for delimitation, as python, indentation should
be of four spaces per level, like the python good practice
recommendations.
问题 1
But when I write code to design a widget (in this case a Box Layout),
Pycharm underlines in red and doesn't execute the code.
解决方案
您必须安装 KV 语言自动完成文件。
- 下载此文件,PyCharm_kv_completion.jar。
- 在 PyCharm 的欢迎 window 的右下角,单击 配置 -> 导入设置。
- Select 您刚下载的 jar 文件,PyCharm 将显示一个对话框,其中勾选了文件类型。单击“确定”。
- 重新启动 PyCharm 以使更改生效。
我想开始学习Kivy。我一直在使用 Pycharm 作为我的 IDE 进行 Python 编程。如何将默认的 python 解释器更改为 kivy 解释器以便 pycharm 可以识别 kivy 代码?
我已经安装了 kivy.app 并创建了符号链接。我还使用 pip 安装了 kivy。在我的 python 程序中,我已经能够从 kivy.app 成功导入应用程序并且它可以工作。但是当我编写代码来设计一个小部件(在本例中为 Box Layout)时,Pycharm 带有红色下划线并且不执行代码。
以下代码工作正常:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class Widget1(BoxLayout):
pass
class MyApp(App):
def build(self):
return Widget1()
if __name__ == "__main__":
MyApp().run()
但是当我编写下面的代码来设计Box Layout时,它不起作用。 Pycharm下划线:Widget1、Button、text和Label(写错是:Unresolved Reference)
<Widget1>
Button:
text: "Please click here"
Label:
text: "Button has not been clicked yet"
如果所有代码都有效,在 运行ning 之后,它应该 return 将一个 Box Layout 一分为二。一部分是一个可点击的按钮,上面写着 "Please click here",另一部分只是一个标签,上面写着 "Button has not been clicked yet"。但是现在当我 运行 时,它只是 return 一个空的框布局(没有标签,没有按钮,没有文本)。
问题 2
the Box Layout code (
<Widget1>
) still doesn't work.
解决方案
1) kv 文件名
由于您没有使用 Kivy Builder 来加载您的 kv codes/file,因此您正在按照名称约定加载 kv codes/file。确保你的 kv 文件名是 my.kv
There are two ways to load Kv code into your application:
By name convention:
Kivy looks for a Kv file with the same name as your App class in lowercase, minus “App” if it ends with ‘App’ e.g:
MyApp -> my.kv
If this file defines a Root Widget it will be attached to the App’s root attribute and used as the base of the application widget tree.
By Builder convention: You can tell Kivy to directly load a string or a file. If this string or file defines a root widget, it will be returned by the method:
Builder.load_file('path/to/file.kv')
or:
Builder.load_string(kv_string)
2) kv 规则上下文
在您的 kv 文件中,在 class 规则后添加 :
(全冒号),<Widget1>
片段
<Widget1>:
A Kv source constitutes of rules, which are used to describe the content of a Widget, you can have one root rule, and any number of class or template rules.
The root rule is declared by declaring the class of your root widget, without any indentation, followed by : and will be set as the root attribute of the App instance:
Widget:
A class rule, declared by the name of a widget class between < > and followed by :, defines how any instance of that class will be graphically represented:
<MyWidget>:
Rules use indentation for delimitation, as python, indentation should be of four spaces per level, like the python good practice recommendations.
问题 1
But when I write code to design a widget (in this case a Box Layout), Pycharm underlines in red and doesn't execute the code.
解决方案
您必须安装 KV 语言自动完成文件。
- 下载此文件,PyCharm_kv_completion.jar。
- 在 PyCharm 的欢迎 window 的右下角,单击 配置 -> 导入设置。
- Select 您刚下载的 jar 文件,PyCharm 将显示一个对话框,其中勾选了文件类型。单击“确定”。
- 重新启动 PyCharm 以使更改生效。