我怎样才能避免在python / kivy中重复?

时间:2016-09-03 21:06:18

标签: python kivy

我一直在尝试制作一个应用程序,其中有许多功能与每个按钮相关联。这样,如果我有70个按钮,我有70个不同的功能。我想添加相应的值,当我点击相应的按钮时,到相应的变量标签(我正在使用numericproperty)。由于此函数之间的变化仅在此数值中,我想以比我更加智能的方式进行此操作。如果没有我必须重复我的代码,有没有人建议更好的方法呢?非常感谢大家,我的部分代码也是如此。

的.py

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)

[...]

和.kv

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]

我有一个类表单,它只是一个标签numericpropertie,它在单击相应按钮时更改了值。由于更改仅在此标签中,因此我将锁定如何从文本标签中获取价格值,并从文本按钮中获取flavor名称。非常感谢。

1 个答案:

答案 0 :(得分:0)

对于你的.kv文件,我建议使用像

这样的类
Route::get('/edisi/{id}', 'JurnalController@store');
Route::resource('jurnals', 'JurnalController');

然后在你的代码中你可以使用MyButton的实例,它可以稍微缩小你的代码。