How can I avoid repetition in python/kivy?
<p>I have been trying to make an app that have many functions associate to one buttons each. This way, if I have 70 buttons I have 70 different functions. I want to add, the respective value, when I click respective button, to a respective variable label (I am using numericproperty). As the change between this functions is only in this numeric value, I would like to make this in a more inteligent way than I did. Have anybody one suggestion of better way to do it without I have to repeat my code? Very thanks everybody, and part of my code is bellow.</p>

<p>.py</p>

<pre><code>class SegundoScreen(Screen):
    def __init__(self, **kwargs):
        self.name = 'dois'
        super(Screen,self).__init__(**kwargs)

    def mucarela(self, *args):
        screen4 = self.manager.get_screen('carrinho')
        screen4.btn1 = BubbleButton(text="MuÃ§arela", font_size='20dp', size_hint=(1,None), background_normal='1.png', background_down='2.png')
        screen4.lb1 = Label(text="25,00", font_size='20dp', size_hint=(1,None))
        screen4.ids.lb5.value += 25
        screen4.ids.grid.add_widget(screen4.btn1)
        screen4.ids.grid.add_widget(screen4.lb1)

    def catupiry(self, *args):
        screen4 = self.manager.get_screen('carrinho')
        screen4.btn2 = BubbleButton(text="Catupiry",font_size='20dp', size_hint=(1,None), background_normal='2.png', background_down='1.png')
        screen4.lb2 = Label(text="25,00",font_size='20dp', size_hint=(1,None))
        screen4.ids.lb5.value += 25
        screen4.ids.grid.add_widget(screen4.btn2)
        screen4.ids.grid.add_widget(screen4.lb2)

    def peru(self, *args):
        screen4 = self.manager.get_screen('carrinho')
        screen4.btn2 = BubbleButton(text="Peito de peru",font_size='20dp', size_hint=(1,None), background_normal='1.png', background_down='2.png')
        screen4.lb2 = Label(text="95,00",font_size='20dp', size_hint=(1,None))
        screen4.ids.lb5.value += 35
        screen4.ids.grid.add_widget(screen4.btn2)
        screen4.ids.grid.add_widget(screen4.lb2)
</code></pre>

<p>[...]</p>

<p>and .kv</p>

<pre><code>StackLayout:
    orientation: 'tb-lr'
    ScrollView:
        size_hint: (1, .9)
        pos_hint:{'x': .0, 'y': .0}
        GridLayout:
            cols: 2
            padding: 45, 50
            spacing: 25, 50
            size_hint: (1, 1)
            size_hint_y:  None
            height: self.minimum_height
            width: 500
            Label:
                text: "[b]Escolha[/b]\n[i]Sabor[/i]"
                markup: True
                font_size: '20dp'
                size_hint_y: None
                height: self.texture_size[1]
            Label:
                text: "[b]PreÃ§o\n[/b] [i](R$)[/i]"
                markup: True
                font_size: '20dp'
                size_hint_y: None
                height: self.texture_size[1]

        Button:
            text: "[b]MuÃ§arela[/b]\n[i]MuÃ§arela, tomate\n e orÃ©gano[/i]"
            markup: True
            font_size: '20dp'
            size_hint_y: None
            height: self.texture_size[1]
            background_normal:'1.png'
            background_down:'2.png' 
            on_press: root.mucarela()

        Label:
            text: "25,00"
            markup: True
            font_size: '20dp'
            size_hint_y: None
            height: self.texture_size[1]

        Button:
            text: "[b]Catupiry[/b]\n[i]Catupiry, azeitona\n e tomate[/i]"
            markup: True
            font_size: '20dp'
            size_hint_y: None
            height: self.texture_size[1]
            background_normal:'2.png'
            background_down:'1.png'
            on_press: root.catupiry()

        Label:
            text: "25,00"
            markup: True
            font_size: '20dp'
            size_hint_y: None
            height: self.texture_size[1]

        Button:
            text: "[b]Peito de peru[/b]\n[i]MuÃ§arela, peito de peru\n e tomate[/i]"
            markup: True
            font_size: '20dp'
            size_hint_y: None
            height: self.texture_size[1]
            background_normal:'1.png'
            background_down:'2.png'
            on_press: root.peru()

        Label:
            text: "35,00"
            markup: True
            font_size: '20dp'
            size_hint_y: None
            height: self.texture_size[1]

        Button:
            text: "[b]Portuguesa[/b]\n[i]MuÃ§arela, presunto,\n cebola e ovo[/i]"
            markup: True
            font_size: '20dp'
            size_hint_y: None
            height: self.texture_size[1]
            background_normal:'2.png'
            background_down:'1.png'
            on_press: root.portuguesa()

        Label:
            text: "27,00"
            markup: True
            font_size: '20dp'
            size_hint_y: None
            height: self.texture_size[1]

        Button:
            text: "[b]Toscana[/b]\n[i]Calabresa moÃ­da, muÃ§arela\ne parmesÃ£o[/i]"
            markup: True
            font_size: '20dp'
            size_hint_y: None
            height: self.texture_size[1]
            background_normal:'1.png'
            background_down:'2.png'
            on_press: root.toscana()

        Label:
            text: "35,00"
            markup: True
            font_size: '20dp'
            size_hint_y: None
            height: self.texture_size[1]
</code></pre>

<p>and I have more one class form that is just one label numericpropertie whose change the value when respective button has clicked. As the change is only in this label, I am locking for how can I take the price value from the text label, and flavor name from the text button. Very thanks.</p>

<p>for your .kv file I recommend using Classes like</p>

<pre><code>&lt;MyButton@Button&gt;:
        markup: True
        font_size: '20dp'
        size_hint_y: None
</code></pre>

<p>then in your code you could use instances of MyButton which could minify your code a little bit.</p>

python kivy