2017-10-30 4 views
0

画像をボタンに設定するにはbackground_normalを使用していますが、その結果、画像が伸びています。KIVY:Buttonの画像が伸びています

Button: 
     background_normal:'this.jpg' 

ボタンの内側に画像を配置する方法はありますか?また、私たちがPythonでどうやってやるか教えてください。

答えて

1

高さと、ボタンの幅が等しくないので、この

の.pyを試してみてくださいstetchedだ:

from kivy.uix.button import Button 
from kivy.properties import StringProperty 

class NewButton(Button): 
    pass 
    #but if you want to set the image of the button remove the pass and uncomment the following: 
    #image_path = StringProperty('') 

    #def __init__(self,image_path, **kwargs): 
     #super(NewButton, self).__init__(**kwargs) 
     #self.image_path = image_path 

.kv

<NewButton>: 
    background_color: 0,0,0,0 
    canvas: 
     Rectangle: 
      size: (40,40) # choose a value of a which fit the most with your button 
      pos: (self.pos[0]+self.size[0]/2.0) - 20, (self.pos[1]+self.size[1]/2.0) - 20 
      source: root.image_path 
    on_press: self.background_color = (1,1,1,1) 
    on_release: self.background_color = (0,0,0,0) 

お知らせどのようにかっこを入れてkvのクラスNewButtonを定義する

更新

<NewButton>: 
    background_color: 0,0,0,0 
    canvas: 
     Rectangle: 
      size: (a,b) # choose a value of a which fit the most with your button 
      pos: (self.pos[0]+self.size[0]/2.0) - a/2.0, (self.pos[1]+self.size[1]/2.0) - b/2.0 
      source: root.image_path 
    on_press: self.background_color = (1,1,1,1) 
    on_release: self.background_color = (0,0,0,0) 

キャンバスにsizeposをご覧くださいabの値を選択し、その後sizepos

+0

あなたは私たちのpythonを使用して、これを行うことができる方法を教えてしまうの設定? –

+0

** .py **でできることは、この種のボタンの新しいクラスを定義し、**。kv **の中に新しいクラスを定義し、このボタンのビューを設定することです。 –

+0

の私の答えを編集するので、私は私のイメージの場所をbackground_normalまたはimage_pathに渡すでしょうか? –

関連する問題