Reputation: 1
I am looking for a way to create a row of buttons in kivy. I am fairly new to kivy so this is what I came up with.
My current code is:
class StackGameApp(App):
def build(self):
layout = FloatLayout()
b0 = Button(pos_hint={'x': 0, 'center_y': .1}, size_hint=(.1, .1),text= '0')
b1 = Button(pos_hint={'x': .1, 'center_y': .1}, size_hint=(.1, .1),text= '1')
b2 = Button(pos_hint={'x': .2, 'center_y': .1}, size_hint=(.1, .1),text= '2')
b3 = Button(pos_hint={'x': .3, 'center_y': .1}, size_hint=(.1, .1),text= '3')
b4 = Button(pos_hint={'x': .4, 'center_y': .1}, size_hint=(.1, .1),text= '4')
b5 = Button(pos_hint={'x': .5, 'center_y': .1}, size_hint=(.1, .1),text= '5')
b6 = Button(pos_hint={'x': .6, 'center_y': .1}, size_hint=(.1, .1),text= '6')
b7 = Button(pos_hint={'x': .7, 'center_y': .1}, size_hint=(.1, .1),text= '7')
b8 = Button(pos_hint={'x': .8, 'center_y': .1}, size_hint=(.1, .1),text= '8')
b9 = Button(pos_hint={'x': .9, 'center_y': .1}, size_hint=(.1, .1),text= '9')
layout.add_widget(b0)
layout.add_widget(b1)
layout.add_widget(b2)
layout.add_widget(b3)
layout.add_widget(b4)
layout.add_widget(b5)
layout.add_widget(b6)
layout.add_widget(b7)
layout.add_widget(b8)
layout.add_widget(b9)
return layout
Which returns a row of buttons at the bottom of the screen labeled 0-9. I will be coding the buttons to return the numbers 0-9, but that hasn't been done yet.
I am almost certain there is a better, easier way of doing this but i just don't know what it is.
Upvotes: 0
Views: 5070
Reputation: 191
You should definitely learn the basics of loops first like jligeza mentioned, but essentially, you need to do something along the following lines:
for i in range(0,10):
layout.add_widget(Button(text=str(i))
Note that the reason that you were getting an error with your for x in 10
comment is because just as the error says an integer is not iterable. Instead, if you use range(0,10), what this is doing is it is iterating through the following list: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Upvotes: 3