2016-09-03 3 views
0

많은 기능이 하나의 버튼에 각각 연결된 앱을 만들려고했습니다. 이렇게하면 70 개의 버튼이 있으면 70 개의 다른 기능을 사용할 수 있습니다. 각각의 값을 각각의 변수 레이블 (각 버튼을 클릭 할 때 numericproperty를 사용)에 추가하고 싶습니다. 이 함수들 사이의 변화는이 숫자 값에만 있기 때문에, 나는 이것을보다 더 강렬한 방식으로 만들고 싶습니다. 아무도 내 코드를 반복하지 않고도 더 나은 방법을 제안 할 수 있습니까? 모든 사람들에게 매우 감사하며, 제 코드의 일부는 우스꽝 스럽습니다. 값 변경 한 라벨이다 numericpropertiepython/kivy에서 반복을 피할 수있는 방법은 무엇입니까?

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] 

및 I는 둘 이상의 클래스 형태를 가질 각 버튼이 클릭되었습니다. 변경 사항은이 라벨에만 있으므로 텍스트 라벨에서 가격 값을 가져오고 텍스트 버튼에서 이름을 취할 수있는 방법은 잠겨 있습니다. 정말 고마워. 당신의 .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 

같은 클래스를 사용하는 것이 좋습니다.

관련 문제