Search code examples
kivykivy-language

Application name in kivy


I have two applications with one kv file. Between both applications is different only in app class name. Application A gives good result, but application B is bad. Where is problem?

application A:

import kivy
kivy.require('1.0.5')
from kivy.lang.builder import Builder

from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.app import App

class MyW(GridLayout):
    pass

class ShowApp(App):
def build(self):
    Builder.load_file('d:\\MyPgm\\Python\\kivy\\ControlShow    \\ControlShow.kv')
    return MyW()


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

application B:

import kivy
kivy.require('1.0.5')
from kivy.lang.builder import Builder

from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.app import App

class MyW(GridLayout):
    pass

class ControlShowApp(App):
def build(self):
    Builder.load_file('d:\\MyPgm\\Python\\kivy\\ControlShow    \\ControlShow.kv')
    return MyW()


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

KV file:

<MyW>
    cols: 2
    rows: 2
    Button:
      id: label1
      text: 'B1'
    Button:
      id: label2
      text: 'B2'
    Button:
      id: label3
      text: 'B3'
    Button:
      id: label4
      text: 'B4'

Solution

  • The problems are as follow:

    1. A class rule, declared by the name of a widget class between < > and followed by : e.g. <MyW>:
    2. The indentation is 4 spaces

    Example

    ControlShow.kv

    #:kivy 1.10.0
    
    <MyW>:
        cols: 2
        rows: 2
        Button:
            id: label1
            text: 'B1'
        Button:
            id: label2
            text: 'B2'
        Button:
            id: label3
            text: 'B3'
        Button:
            id: label4
            text: 'B4'
    

    ShowApp.py

    from kivy.lang.builder import Builder
    from kivy.uix.gridlayout import GridLayout
    from kivy.app import App
    
    
    class MyW(GridLayout):
        pass
    
    
    class ShowApp(App):
        def build(self):
            Builder.load_file('d:\\MyPgm\\Python\\kivy\\ControlShow    \\ControlShow.kv')
            return MyW()
    
    
    if __name__ == '__main__':
        ShowApp().run()
    

    ControlShowApp.py

    from kivy.lang.builder import Builder
    from kivy.uix.gridlayout import GridLayout
    from kivy.app import App
    
    
    class MyW(GridLayout):
        pass
    
    
    class ControlShowApp(App):
        def build(self):
            Builder.load_file('d:\\MyPgm\\Python\\kivy\\ControlShow    \\ControlShow.kv')
            return MyW()
    
    
    if __name__ == '__main__':
        ControlShowApp().run()
    

    Output

    enter image description here