Search code examples
python-3.xpycharmkivykivy-language

How to Use Kivy (1.10.1) Interpreter in Pycharm (Community Edition 2018.3.5)


I want to start learning Kivy. I have been using Pycharm as my IDE for Python programming. How can I change the default python interpreter to a kivy interpreter so pycharm can recognise kivy codes?

I have installed kivy.app and created symlinks. I have also installed kivy using pip. In my python program, I have been able to successfully import App from kivy.app and it works. 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.

The following code works fine:

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()

But when I write the following code to design the Box Layout, it doesn't work. Pycharm underlines: Widget1, Button, text and Label (Error written is: Unresolved Reference)

<Widget1>
    Button:
        text: "Please click here"
    Label:
        text: "Button has not been clicked yet"

If all the codes work, after running, it's supposed to return a Box Layout split into two. One part is a clickable button with "Please click here" written on it and the other part is just a label with "Button has not been clicked yet" written on it. But now when I run, it just returns an empty Box Layout (no label, no button, no text).


Solution

  • Question 2

    the Box Layout code (<Widget1>) still doesn't work.

    Solution

    1) kv Filename

    Since you are not using Kivy Builder to load your kv codes/file, therefore you are loading kv codes/file by name convention. Make sure your kv filename is my.kv

    Kv language » How to load 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 Rule Context

    In your kv file, add : (full colon) after class rule, <Widget1>

    Snippets

    <Widget1>:
    

    Kv language » Rule Context

    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.

    Question 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.

    Solution

    You have to install KV Language auto-completion file.

    1. Download this file, PyCharm_kv_completion.jar.
    2. At bottom-right corner of PyCharm’s Welcome window, click Configure -> Import Settings.
    3. Select the jar file you just downloaded and PyCharm will present a dialog with filetypes ticked. Click OK.
    4. Restart PyCharm for the changes to take effect.