【问题标题】:Kivy: How to make widget behave like overflow:hiddenKivy:如何使小部件表现得像溢出:隐藏
【发布时间】:2013-05-19 08:19:20
【问题描述】:

我想在 Kivy 中创建带有许多矩形按钮的 GridLayout,其中包含一些不同大小的自定义图像。为此,我想缩放图像(我想这样做的方式如下所示),然后裁剪/隐藏溢出小部件边框的部分。调整大小效果很好,但我不知道如何隐藏/裁剪矩形之外的部分。例如:如果小部件的尺寸为 10 像素 x 10 像素,而我的图像为 100 像素 x 200 像素,我会将其调整为 10 像素 x 20 像素并在小部件中居中,但下方和上方的 5 像素将可见。我不要那个。 :) 云有人帮我解决这个问题吗?

class PlaceIcon(Widget):
    def __init__(self,image_path, **kwargs):
        super(PlaceIcon, self).__init__(**kwargs)
        self.bind(size=self.adjust_size)
        self.image = Image(source=image_path)
        self.image_path = image_path  

    def adjust_size(self,*args):
        (a,b) = self.image.texture.size
        (x,y) = self.size
        (x1,y1) = self.pos
        if x > y:
            scale = x/a
        else:
            scale = y/b
        x1 -= (scale*a-x)/2
        y1 -= (scale*b-y)/2
        with self.canvas:
            self.canvas.clear()
            self.background = Rectangle(texture=self.image.texture, pos=(x1,y1), size=(scale*a,scale*b))

【问题讨论】:

    标签: python kivy


    【解决方案1】:

    我会说看看 Texture.get_region: http://kivy.org/docs/api-kivy.graphics.texture.html#kivy.graphics.texture.Texture.get_region

    因此您可以只使用您想要显示和传递的纹理部分,就好像它是完全不同的纹理一样。

    另一种方法是使用 StencilView,但我认为这里的 get_region 更好。

    【讨论】:

    • 是的,这正是我所需要的!谢谢。
    猜你喜欢
    • 1970-01-01
    • 2017-06-18
    • 2010-12-06
    • 1970-01-01
    • 2013-05-08
    • 2017-12-28
    • 2020-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多