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
JavaScript
x
32
32
1
from kivy.app import App
2
from kivy.uix.label import Label
3
from kivy.uix.gridlayout import GridLayout
4
from kivy.uix.textinput import TextInput
5
from kivy.uix.button import Button
6
from kivy.uix.widget import Widget
7
from kivy.properties import ObjectProperty
8
9
class MyGridLayout(Widget):
10
11
12
name = ObjectProperty(None)
13
userId = ObjectProperty(None)
14
15
16
17
def press(self):
18
name = self.name.text
19
userId = self.id.text
20
21
# print(f'hello {name} , your id {id}')
22
# self.add_widget(Label(text = f'name : {name} , id : {id}'))
23
self.name.text = ""
24
self.id.text = ""
25
26
class MyApp(App):
27
def build(self):
28
return MyGridLayout()
29
30
if __name__ == '__main__' :
31
MyApp().run()
32
My my.kv file is:
JavaScript
1
29
29
1
<MyGridLayout>
2
3
name:name
4
userId:userId
5
6
GridLayout:
7
cols:1
8
size: root.width , root.height
9
GridLayout:
10
cols:2
11
12
Label:
13
text: "user Name"
14
TextInput:
15
id :Name
16
multiline:False
17
18
Label:
19
text: "user id"
20
TextInput:
21
id:userId
22
multiline:False
23
24
25
Button:
26
text: "Submit"
27
font_size: 32
28
on_press: root.press()
29
Advertisement
Answer
I use Builder.load_string() and paste my .kv code in it and that’s work