user13429019
user13429019

Reputation:

Kivy Issues with .py and .kv

I'm using Windows 10, Python 3.7.9 My code:

In Main.py:

from kivy.app import App
from kivy.uix.widget import Widget


class MyGrid(Widget):
    pass


class Main(Widget):
    def build(self):
        return Main()


if __name__ == "__main__":
    Main().run()

In main.kv:

<Main>
    GridLayout:
        cols:1

        GidLayout:
            cols:2

            Label:
                text: "Name: "

            TextInput:
                multiline:False

            Label:
                texxt: "Email: "
            
            TextInput:
                multiline:False

        Button:
            text:"Submit"

Error(1):

In the .py file, 'Main' in 'Main().run()' is underlined with:

Instance of 'Main' has no 'run' member

Error(2):

In the .kv file '' is underlined with:

Kivy files require #:Kivy

I can't figure out how to fix, any help is appreciated

Upvotes: 2

Views: 244

Answers (2)

Attila Toth
Attila Toth

Reputation: 469

I see two bugs, one in the kv code, and the second in the python code.

In the kv code change the <Main> to <MyGrid>. MyGrid class is the root widget, not the Main class. The Main class is only responsible to build the widget tree.

In the python code, change the return of the build method in the Main class so that it returns a MyGrid instance. And the Main should inherit from the App class, not from the Widget class.

class Main(App):
    def build(self):
        return MyGrid()

Upvotes: 1

John Anderson
John Anderson

Reputation: 38937

According to the documentation, a kv file:

Syntax of a kv File A Kivy language file must have .kv as filename extension.

The content of the file should always start with the Kivy header, where version must be replaced with the Kivy language version you’re using. For now, use 1.0:

#:kivy `1.0`

# content here

Then, in your py file. an application must extend App not Widget. Also, the build() method must return a Widget, not an instance of the App. And, if your App is named Main, then you should choose a different name for the root widget of your App.

The rules in the kv file describe how to build widgets, and cannot be applied to building an App. They can describe the building of the root widget of the App, but not the App itself.

Upvotes: 1

Related Questions