Kivy Image Child Class产生两个图像

时间:2016-10-01 00:59:31

标签: python image canvas graphics kivy

我认为这可能是一个简单的错过了解kivy更深层次的东西,但我有一个RoundedImage类产生两个图像,其中一个是原始没有圆角和一个圆角离开屏幕一点。怎么回事?

截图: http://imgur.com/gallery/IRYfr

我认为这可能与继承Image?

有关
class RoundedImage(Image,StyleUnit):

    _styleist = RadialGradientStyleist    
    _radius = [20]
    _source = ''
    _style = None
    def __init__(self,**kwargs):

        super(RoundedImage,self).__init__(**kwargs)
        #self._source = source
        #self.initalizeStyle()

        with self.canvas:
            StencilPush()
            self.m_rect = RoundedRectangle( size = self.norm_image_size , \
                                            pos=self.center, \
                                            radius=self._radius)
            StencilUse()
            self.rect = Rectangle(  size = self.norm_image_size , \
                                    pos = self.center, \
                                    texture = self.texture)
            StencilUnUse()
            StencilPop()
            #Color(1,1,1)
            #self.line = Line(   rounded_rectangle=self.pos+self.size+self._radius,
            #        width=10)


        self.bind(pos = self.update_rect,
                  size = self.update_rect)

    def update_rect(self,*args):
        self.m_rect.pos = self.center
        self.m_rect.size = self.norm_image_size
        self.rect.pos = self.center
        self.rect.size = self.norm_image_size

应用程序代码很简单:

class ProfilesApp(App):

    def build(self):
       profile = RoundedImage( source = source,#self.imageLocation,\
                                    allow_stretch=True)
       return profile

profileApp = ProfilesApp()
profileApp.run()

2 个答案:

答案 0 :(得分:1)

没有导入,没有其他必需的类,虽然我在某处看到了圆角矩形,但我不知道该自定义类的功能,所以没有可运行的代码。无论如何,让我们一起工作。

是的,因为它的子类化 - 图片已经放在画布中,你使用Rectangle(texture=<here>)中的纹理。而不是删除图像的子类,为source使用Rectangle关键字参数,如下所示:

Rectangle(source=<path to image>)

然后,虽然您在继承自source的类中使用Image,但您需要更改它并从kwargs中获取source

self.source = kwargs.get('source')

然后代替Rectangle(texture=self.texture)Rectangle(source=self.source)

答案 1 :(得分:1)

好的,所以最终出现的问题是没有在模板说明中删除掩码。

我最终将此添加到RoundedRectangle的.kv定义

'''
<-RoundedImage>:
canvas:
Color:
rgb: self.color
StencilPush
RoundedRectangle:
size: self.norm_image_size
pos: self.center[0] - self.norm_image_size[0]/2.0,self.center[1] - self.norm_image_size[1]/2.0
radius: self._radius
StencilUse
Rectangle:
texture: self.texture
size: self.norm_image_size
pos: self.center[0] - self.norm_image_size[0]/2.0,self.center[1] - self.norm_image_size[1]/2.0
StencilUnUse
RoundedRectangle:
size: self.norm_image_size
pos: self.center[0] - self.norm_image_size[0]/2.0,self.center[1] - self.norm_image_size[1]/2.0
radius: self._radius
StencilPop

'''