Kivy 圆角(仅使用 Python)

我有这个小 Kivy 应用程序(Python 版本:3.7,Kivy 版本:1.11.1):


代码#1


from kivy.app import App

from kivy.lang import Builder

from kivy.config import Config

from kivy.uix.floatlayout import FloatLayout


Config.set("graphics", "width", "500")

Config.set("graphics", "height", "300")


kv = """

<RoundedCornerLayout@FloatLayout>:

    background_color: 0,0,0,0

    canvas.before:

        Color:

            rgba: (.4,.4,.4,1)

        RoundedRectangle:

            pos: self.pos

            size: self.size

            radius: [(40, 40), (40, 40), (20, 20), (20, 20)]

"""


Builder.load_string(kv)



class RoundedCornerLayout(FloatLayout):

    def __init__(self):

        super().__init__()

        self.size_hint = (None, None)

        self.size = (400, 200)

        self.pos_hint = {"center_x": 0.5, "center_y": 0.5}



class MainApp(App):

    def build(self):

        return RoundedCornerLayout()



if __name__ == "__main__":

    MainApp().run()

可爱,不是吗?


现在,让我们尝试仅使用 Python 获得相同的结果。我正在尝试使用以下代码:


代码#2


from kivy.app import App

from kivy.config import Config

from kivy.graphics import Color

from kivy.graphics import Rectangle

from kivy.uix.floatlayout import FloatLayout


Config.set("graphics", "width", "500")

Config.set("graphics", "height", "300")



class RoundedCornerLayout(FloatLayout):

    def __init__(self):

        super().__init__()

        self.size_hint = (None, None)

        self.size = (400, 200)

        self.pos_hint = {"center_x": 0.5, "center_y": 0.5}


        self.background_color = (0, 0, 0, 0)

        self.canvas.before.add(Color(.4, .4, .4, 1))

        self.canvas.before.add(Rectangle(

            pos=self.pos,

            size=self.size,

            radius=[(40, 40), (40, 40), (20, 20), (20, 20)]))


够公平的,我想。


但后来我得到了这个结果:


在此处输入图像描述


据我所知,两条指令(代码#1 和代码#2)都说相同,但方式不同。科学证明,事实并非如此。


...所以我想在这里理解的是,我的问题的重点是:Code#1 和 Code#2 之间的功能区别是什么?为什么他们显示不同的结果?将 Code#1“翻译”为纯 Python 代码的正确方法是什么?


忽略这样一个事实,即仅保留 kivy 代码是最简单的解决方案。我在这里需要的是理解这种行为,解释我的理由会不必要地扩展这个问题,让我们说你只能控制你所理解的。


SMILET
浏览 262回答 1
1回答

拉丁的传说

您有 2 个错误:该项目不是 Rectangle 而是 RoundedRectangle。在 .kv 中,如果绘画中使用的属性因绑定而发生更改,则会重新绘制画布,但是在 Python 中,您必须显式地进行该绑定。from kivy.app import Appfrom kivy.config import Configfrom kivy.graphics import Color, RoundedRectanglefrom kivy.uix.floatlayout import FloatLayoutConfig.set("graphics", "width", "500")Config.set("graphics", "height", "300")class RoundedCornerLayout(FloatLayout):&nbsp; &nbsp; def __init__(self):&nbsp; &nbsp; &nbsp; &nbsp; super().__init__()&nbsp; &nbsp; &nbsp; &nbsp; with self.canvas.before:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Color(0.4, 0.4, 0.4, 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.rect = RoundedRectangle(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pos=self.pos,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; size=self.size,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; radius=[(40, 40), (40, 40), (20, 20), (20, 20)],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; self.bind(pos=lambda obj, pos: setattr(self.rect, "pos", pos))&nbsp; &nbsp; &nbsp; &nbsp; self.bind(size=lambda obj, size: setattr(self.rect, "size", size))&nbsp; &nbsp; &nbsp; &nbsp; self.size_hint = (None, None)&nbsp; &nbsp; &nbsp; &nbsp; self.size = (400, 200)&nbsp; &nbsp; &nbsp; &nbsp; self.pos_hint = {"center_x": 0.5, "center_y": 0.5}&nbsp; &nbsp; &nbsp; &nbsp; self.background_color = 0, 0, 0, 1class MainApp(App):&nbsp; &nbsp; def build(self):&nbsp; &nbsp; &nbsp; &nbsp; return RoundedCornerLayout()if __name__ == "__main__":&nbsp; &nbsp; MainApp().run()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python