2016-03-22 109 views
2

我創建一個自定義按鈕,如下更改自定義按鈕屬性

#: import icon kivy.garden.iconfonts.icon 
<[email protected]>: 
    background_normal: 'icons/backg.png' 
    RelativeLayout: 
     size: self.parent.width, self.parent.height 
     top: self.parent.top 
     right: self.parent.right 
     Label: 
     canvas.before: 
      Color: 
       rgba: 1,1,1,1 
      Rectangle: 
       pos: self.pos 
       size: self.size 
       source: 'icons/restaurant.png' 
     color: 0,0,0,1 
     id: icon_name 
     markup: True 
     font_size: '20dp' 
     text: '{}'.format(icon('ion-settings', 38)) 
     pos_hint: {'center_y': .5, 'right': .25} 
     size_hint: .18, .9 
     Label: 
     text:'Change Settings' 
     id: label 
     color: 0,0,0,1 
     text_size: self.size 
     halign: 'left' 
     valign: 'middle' 
     font_size: '20dp' 
     pos_hint: {'center_y': .5, 'right': 1} 
     size_hint: .7, .9 

我希望能夠通過這個(Custom_Button)作爲一個孩子不同的佈局,和一些屬性更改爲我想要什麼。這就是我的意思。 例如,

GridLayout: 
    custom_Button 
    custom_Button 
    custom_Button 

但是我想能夠明確地更改標籤的圖標在custom_Button第一個標籤,也是在第二個標籤的文本,因此,對於cutom_Button一個的三個實例會顯示不同的圖標和文字。我真的不知道如何實現這一點。所以請我需要一些幫助。 示例代碼將非常有幫助。在此先感謝...

回答

3

首先,請將類名更改爲某種標準,例如CustomButton。然後,定義包含icon_source一個新的屬性:

<[email protected]>: 
    icon_source: 'icons/restaurant.png' 
... 

,並指它以後:

Rectangle: 
    pos: self.pos 
    size: self.size 
    source: root.icon_source 

然後,它的簡單改變這種對每一個實例,無論是在kv或蟒蛇:

GridLayout: 
    CustomButton: 
     icon_source: 'something/else.png' 
+0

正是我需要的。非常感謝 –