2016-09-03 4 views
0

私は、多くの機能がそれぞれ1つのボタンに関連付けられているアプリを作ろうとしています。この方法では、私は70のボタンがある場合、私は70の異なる機能があります。私はそれぞれの値を、それぞれのボタンをクリックすると、それぞれの変数ラベル(私は数値プロパティを使用しています)に追加したいと思います。この関数の間の変更はこの数値のみであるので、私はこれをもっと賢明な方法で行いたいと思います。誰かに私のコードを繰り返さなくても良い方法を提案してもらえますか?誰もが非常に感謝し、私のコードの一部は腹が立っています。変更値だけ1ラベルnumericpropertieあるpython/kivyで繰り返しを避けるにはどうすればいいですか?

の.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] 

と私はより多くの1つのクラスのフォームを持っていますそれぞれのボタンがクリックされました。このラベルにのみ変更があるので、私はテキストラベルから値段を取り、テキストボタンからフレーバー名を取得する方法をロックしています。どうもありがとう。あなたの.kvファイルの

+0

このコードは、動作しているスクリプトなので、[コードレビュー](https://codereview.stackexchange.com/)の方が良いかもしれません。 –

+0

申し訳ありませんが、私はstackoverflowで多くの経験がありません。私はそこでそれを聞くことができますか?私はただそれを知っている。 – victorcd

+0

[コードレビュー](https://codereview.stackexchange.com/)は、「書き留めたコードを改善する」*、スタックオーバーフローはプログラミングに関する質問であり、コードのレビューではありません。ここでも答えが出るかもしれませんが、この種の質問に対してはコードレビューが行われます。 –

答えて

0

私は、あなたのコード内であなたのコードを少し縮小化可能性にMyButtonのインスタンスを使用することができ

<[email protected]>: 
     markup: True 
     font_size: '20dp' 
     size_hint_y: None 

のようなクラスを使用することをお勧めします。

関連する問題