官术网_书友最值得收藏!

  • Kivy Cookbook
  • Hugo Solis
  • 617字
  • 2021-07-16 20:39:54

Relating Python code and the Kv language

This recipe will teach you how to relate the Kv language to Python code. Kivy provides a design language specifically geared toward easy and scalable GUI design. The Kv language separates the interface design from the application logic, adhering to the separation of concerns principle, where the application remains in Python and the design remains in the Kv language.

Getting ready

This recipe will create the same interface as the recipe Building your interfaces, but now using the Kv language; hence, it could be educational to look the code in there to make some comparisons. Again we are using gedit, just because it comes with almost all GNU/Linux distros.

How to do it…

These steps will generate our Kivy interface using the Kv language:

  1. Open a new file in gedit and save it as e4.py.
  2. Make a rule for the label.
  3. Provide the text for the label:
    <Label>:
        text: 'Hello World' 
  4. Open a new file in gedit and save it as e4.py.
  5. Import the Kivy framework.
  6. Provide a subclass to the App class.
  7. Implement its build() method, so it returns a widget instance.
  8. Instantiate this class and call its run() method:
    import kivy
    kivy.require('1.9.0') # Kivy ver where the code has been tested!
    from kivy.app import App
    from kivy.uix.label import Label
    class e4App(App):
        def build(self): 
            return Label() 
    
    if __name__ == '__main__':
        e4App().run() 

How it works…

Well, let's see the code in detail. The first line for the file e4.kv is:

<Label>:

This line creates the rule for a Label. The second one is:

text: 'Hello World'

In this, we define the text property for the Label with the value 'Hello World'.

Now, the first four lines of the Python file are the common ones to use Kivy in Python, and we already reviewed them in this chapter recipe Building your Interfaces. Moving on to the fifth line:

class e4App(App):

This is where we define the base class of our Kivy app. You should only ever need to change the name of your app e4App in this line, and here is where the relationship between the Kv language and the Python code occurs. What happens is that Kivy looks for a file named e4.kv, which could be present or not. The sixth line is:

def build(self):

This is the function where you initialize and return your root widget. This is what we do on the seventh line:

return Label()

Here we initialize a Label and returned its instance. This Label is the root widget of this app, and it must be the same in the KV file.

Now, on to the portion that makes our app come to life is in the eighth and ninth lines:

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

Here, the class e4App is initialized, and its run() method is called. This initializes and starts our Kivy app.

There's more…

The filename of the KV file should be such that adding the name of the KV file and App will become the name of the subclass of the App class. For example, if you change the name to Win1App, then you should also change the KV filename to Win1.kv.

Something to point out is that we can incorporate the KV code inside the Python file with the class Builder. We just add a few lines between the fourth and fifth lines in the Python code to import the Builder package and the override of the method as follows:

from kivy.lang import Builder
Builder.load_string('''
 <Label>: 
 text: 'Hello World' 
''')

See also

If you want to run your interface, take a look at our recipe Running your Code and compare the result with the recipe Building your Interfaces.

主站蜘蛛池模板: 石阡县| 深泽县| 江陵县| 大同县| 军事| 扬州市| 鄂托克旗| 余江县| 密山市| 铜川市| 丰顺县| 新兴县| 张掖市| 长海县| 昭苏县| 中山市| 昭通市| 易门县| 德保县| 大渡口区| 长垣县| 电白县| 犍为县| 桐梓县| 寿光市| 曲阜市| 白河县| 石首市| 焉耆| 娱乐| 宝丰县| 嘉峪关市| 滨海县| 金坛市| 廉江市| 玛沁县| 托克托县| 枞阳县| 余姚市| 罗平县| 社会|