KIVY:按钮中的图像被拉伸

时间:2017-10-30 21:48:29

标签: python python-3.x kivy kivy-language

我使用background_normal将图像设置为按钮,但结果是图像被拉伸。

Button:
        background_normal:'this.jpg'

有没有办法在按钮内部有一个居中对齐的图像。还请告诉我我们如何在python中完成它。

1 个答案:

答案 0 :(得分:1)

它因为按钮的高度和宽度不相等而被抄袭,试试这个

<强>的.py

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)

请查看size和画布上的pos选择值ab,然后设置sizepos < / p>

相关问题