I’m trying to make a simple app that takes name, grade, language (just for practice).
Here’s the Code:
JavaScript
x
52
52
1
import kivy
2
from kivy.app import App
3
from kivy.uix.label import Label
4
from kivy.uix.gridlayout import GridLayout
5
from kivy.uix.textinput import TextInput
6
from kivy.uix.button import Button
7
8
9
class Mylays(GridLayout):
10
def __init__(self,**kwargs):
11
super(Mylays,self).__init__(**kwargs)
12
self.top_grid = GridLayout() # Widget above main widget to hold all text and input boxes.
13
self.cols = 2 # no.of columns
14
15
self.top_grid.add_widget(Label(text="Name : ",font_size=20)) # add a widget with "Name:" in it.
16
self.name = TextInput(multiline=False) # make a input box with multiline False
17
self.top_grid.add_widget(self.name) # place the input box to widget
18
19
self.top_grid.add_widget(Label(text="class :"))
20
self.hisclass = TextInput(multiline=False)
21
self.top_grid.add_widget(self.hisclass)
22
23
self.top_grid.add_widget(Label(text="Lang:"))
24
self.lang = TextInput(multiline=False)
25
self.top_grid.add_widget(self.lang)
26
27
self.add_widget(self.top_grid)
28
29
30
self.click = Button(text="Boom!",font_size=25)
31
self.click.bind(on_press=self.buttonfunction)
32
self.add_widget(self.click)
33
34
35
36
def buttonfunction(self, instance):
37
name = self.name.text
38
CLASs = self.hisclass.text
39
langu = self.lang.text
40
x = "Hi {0},Ik you are from {1}. I also likes {2} Language .".format(name,CLASs,langu)
41
self.add_widget(Label(text=x))
42
self.name.text = ""
43
self.hisclass.text = ""
44
self.lang.text = ""
45
46
class firstapp(App):
47
def build(self):
48
return Mylays()
49
50
if __name__ == "__main__":
51
firstapp().run()
52
Although it runs I get the layout all wrong and just the button on the screen with following error:
[WARNING] <kivy.uix.gridlayout.GridLayout object at 0x04254108> have no cols or rows set, layout is not triggered.
Advertisement
Answer
You are getting that error because you are not setting cols
or rows
for a GridLayout
. In this case, the GridLayout
in question is the one created by:
JavaScript
1
2
1
self.top_grid = GridLayout()
2
The fix is to set cols
or rows
for that GridLayout
. Try adding a line like:
JavaScript
1
2
1
self.top_grid.cols = 2
2
just after creating self.top_grid
.