Skip to content

Instantly share code, notes, and snippets.

@rogererens
Created May 11, 2015 20:11
Show Gist options
  • Save rogererens/5499a7d41439408d23da to your computer and use it in GitHub Desktop.
Save rogererens/5499a7d41439408d23da to your computer and use it in GitHub Desktop.
Adding widgets based on settings
from kivy.app import App
from kivy.factory import Factory
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
Builder.load_string('''
<MilkshakeRoot>:
milkshakes: box_with_milkshakes
orientation: 'vertical'
BoxLayout:
id: box_with_milkshakes
orientation: 'horizontal'
Button:
size_hint_y: 0.1
text: 'Reset'
on_press: app.root.reset_numbers()
<MilkshakeWidget>
orientation: 'vertical'
name: ''
number_ordered: 0
Button:
# background_normal: root.name + '_small.png'
on_press: root.increase_number()
Label:
size_hint_y: 0.2
text: root.name
Label:
id: root.name
size_hint_y: 0.4
font_size: '40sp'
text: str(root.number_ordered)
''')
class MilkshakeRoot(BoxLayout):
milkshakes = ObjectProperty(None)
def add_milkshake(self, name):
milkshake = Factory.MilkshakeWidget()
milkshake.name = name
self.milkshakes.add_widget(milkshake)
def reset_numbers(self):
for milkshake in self.milkshakes.children:
milkshake.number_ordered = 0
### speculative function call:
# self.set_number(milkshake.name, 99)
### speculative function definition:
def set_number(self, taste=None, my_special_number=0):
if taste is not None:
self.milkshakes[taste]['number_ordered'] = my_special_number
class MilkshakeWidget(BoxLayout):
def increase_number(self):
self.number_ordered += 1
class MilkshakeManiaApp(App):
def build(self):
root = MilkshakeRoot()
milkshake_names = ['banana', 'chocolate', 'lemon']
for milkshake_name in milkshake_names:
root.add_milkshake(milkshake_name)
return root
if __name__ == '__main__':
MilkshakeManiaApp().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment