Skip to content
Advertisement

Kivy – Error while trying to use my.kv file

I’ve just started learning kivy and I am unable to get a black window when I run this code, and this is an error:

Kivy files require #:kivy !ex

This is my code snippet:

gui_python.py

from kivy.app import App
from kivy.uix.label import Label   
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty

class MyGridLayout(Widget):


     name = ObjectProperty(None)
     userId = ObjectProperty(None)
   


     def press(self):
        name = self.name.text
        userId = self.id.text

        # print(f'hello {name} , your id {id}')
     #    self.add_widget(Label(text = f'name : {name} , id : {id}'))
        self.name.text = ""
        self.id.text = ""

class MyApp(App):
    def build(self):
        return MyGridLayout()

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

My my.kv file is:

<MyGridLayout>

    name:name
    userId:userId

    GridLayout:
        cols:1
        size: root.width , root.height
        GridLayout:
            cols:2

            Label:
                text: "user Name"
            TextInput:
                id :Name
                multiline:False    

            Label:
                text: "user id"
            TextInput:
                id:userId
                multiline:False 


        Button:
            text: "Submit"
            font_size: 32
            on_press: root.press()

enter image description here

Advertisement

Answer

I use Builder.load_string() and paste my .kv code in it and that’s work

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement