简体   繁体   English

如何避免在python / kivy中重复?

[英]How can I avoid repetition in python/kivy?

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. 这样,如果我有70个按钮,则我有70种不同的功能。 I want to add, the respective value, when I click respective button, to a respective variable label (I am using numericproperty). 我想在单击相应按钮时将相应的值添加到相应的变量标签(我正在使用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. 非常感谢大家,下面是我的部分代码。

.py .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)

[...] [...]

and .kv 和.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]

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. 很感谢。

for your .kv file I recommend using Classes like 对于您的.kv文件,我建议使用类似

<MyButton@Button>:
        markup: True
        font_size: '20dp'
        size_hint_y: None

then in your code you could use instances of MyButton which could minify your code a little bit. 那么在您的代码中,您可以使用MyButton的实例,这些实例可以使您的代码最小化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM