Kivy Python Error on KV File – Layout is not Triggered

Question:

I am new to both Python and Kivy, while I was trying out I got the following error, and browsed net but not able to find the issue, could you please explain my error in the below code:

Error:

[INFO              ] [Base        ] Start application main loop
[WARNING           ] `<ui.LoginForm.LoginForm object at 0x02D503E8>` have no cols or rows set, layout is not triggered.
[INFO              ] [Base        ] Leaving application in progress...

Structure:

File & Folder Structure

File: Main.py

    from kivy.app import App
    from ui.LoginForm import LoginForm
    
    class MyApp(App):
            
        def build(self):
            c = LoginForm()
            return c
    
    if __name__ in ( '__main__','__android'):
        MyApp().run()

File: loginform.kv
<LoginForm>
    UserIDtxt: UserIDtxt
    rows: 2
    cols:2
    
    Label:
        text: 'User ID:'
        size_hint_y: None
        height: '40dp'
        
    TextInput:
        id: UserIDtxt
        size_hint_y: None
        height: "40dp"
            
    Label:
        text: "User PW:"
        size_hint_y: None
        height: "40dp"
        
    TextInput:
        id: "UserPWtxt"
        password: True
        size_hint_y: None
        height: "40dp"

File: LoginForm.py

from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder

class LoginForm(GridLayout):
    '''
    classdocs
    '''

    def __init__(self, **kwargs):
        '''
        Constructor
        '''
        super(LoginForm, self).__init__(**kwargs)
        Builder.load_file('ui/loginform.kv')
        
  1. I tried with Builder.load_file, adding UserIDtxt: UserIDtxt to KV file is showing as error, could you please let me learn where my error is. Also, as per kivy specifications, it should load the KV file automatically if filename is same in small letters, why should I use a builder class again.

  2. Also, I receive following log when I comment out UserIDtxt: UserIDtxt in KV file.

    [WARNING ] <ui.LoginForm.LoginForm object at 0x02DA9458> have no cols or rows set, layout is not triggered.
    [INFO ] [Base ] Leaving application in progress…

Thank you.

[EDIT]:
All the things said by @inclement worked, thank you. I made the modifications as follows:

  1. I added the my.kv file with same content as loginform.kv as it is, no change.

  2. Removed build statement from loginForm.py:
    Then I receive the following: app no displaying again, could you please tell me how to add multiple class in KV file and get them to use in respective py files.

    [INFO ] [Base ] Start application main loop
    [WARNING ] <ui.LoginForm.LoginForm object at 0x02DB1420> have no cols or rows set, layout is not triggered.

Asked By: surpavan

||

Answers:

1) Your load_file is in the wrong place (you don’t actually see this error yet because of your other problems)

super(LoginForm, self).__init__(**kwargs)
Builder.load_file('ui/loginform.kv')

Any kv file definitions for your widgets are applied during a widget’s normal __init__ method, which you call with super here. Since you only try to load the kv file after this would have happened, kivy doesn’t know about your rules, so (as per the warning message) the layout isn’t triggered.

You could put the load_file above the super, but even simpler would be to just load all your kv files at the start of your app (e.g. as the first thing during build) or put them in the default kv file as explained below. Performance implications will be minimal, you can change it later if it’s important.

Edit: Maybe you misunderstood what the load does – it only loads the widget definitions from the kv file, it doesn’t apply them to any widgets. That only happens when the widget is instantiated, which is why you need to load the file first.


2) Your UserIDtxt property shouldn’t be uppercase

Kivy language uses the case of the first letter to determine if a line refers to a property or a child widget. In this case, it thinks it’s a child widget (because widget class names should start with capital letters), so your syntax is wrong.


3) Your autoload question:

Also, as per kivi specifications, it should load the kv file automatically if filename is same in small letters

Kivy will automatically try to load a single kv file with a name based on the lowercase form of your App class (without the App suffix if it exists). In this case, your app class is MyApp, so kivy will try to load my.kv, which doesn’t exist.

Answered By: inclement
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.