2016-03-22 163 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

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