有没有一种简单的方法可以在没有图像的情况下为 Kivy 标签、按钮、小部件等添加边框?

2022-01-15 00:00:00 python kivy styling border

问题描述

我正在尝试为 Kivy 按钮添加边框,但它没有按预期工作.对于标签,我的实现似乎没问题,但对于按钮,它会覆盖/清除按钮的标准外观.

如何在不改变正常行为的情况下在按钮上方绘制边框?我想像 ButtonBehavior 一样实现它,这样我就可以使用画布为每个 Kivy 对象添加边框.我称之为 BorderBehavior.

样式虚线,点线仅适用于线宽 1,因为 Kivy 中存在错误(请参阅

解决方案

好的,我找到了解决方法.这是一个命名冲突.

在控制台日志中,我看到在按钮的 BorderImage 中解包边框元组时出现问题.当然,这是行不通的,因为我的边界实现方式不同.也许我可以将列表(上、右、下、左)添加到我的边框实现中,这样我就可以保持相同的名称.但我还没有检查过.

在 python 和 kv 中将我的边框的命名更改为边框修复了问题:

类 BorderBehavior(Widget):边框 = ObjectProperty(无)

现在看起来我想要它:

好的,现在我将检查它是否也适用于其他类(例如 Scatter、Widget、...).如果这行得通,我正在向 Kivy 发出拉取请求.

I'm trying to add a border to Kivy Buttons but it doesn't work as expected. For labels my implementation seems to be OK but for buttons it overrides/clears the standard look of the button.

How can I draw the border above the button with-out changing normal behavior? I'd like to implement it like the ButtonBehavior so I can add a border to every Kivy object with a canvas. I've called it BorderBehavior.

Styling dashed, dotted works only for line width of 1 because there is a bug in Kivy (see https://github.com/kivy/kivy/issues/2037) (Need to figure out what's wrong here later.)

I know that drawing a border is possible with a BorderImage but I'd like to add simple borders with-out an image.

Here is how it looks at the moment:

You can find my source code here (the labels can be dragged just for testing purposes to see that the border is always correctly positioned):

https://gist.github.com/AWolf81/c6796dc2049d9872b2df

解决方案

OK, I've found the fix. It was a naming conflict.

In the console log I saw that there is a problem at unpacking the border tuple in BorderImage of the button. Of course, that's not working because my border is implemented differently. Maybe I can add the list (top, right, bottom, left) to my border implementation so I can keep the same name. But I haven't checked that yet.

Changing the naming of my border to borders in python and in kv fixed the problem:

class BorderBehavior(Widget):
    borders = ObjectProperty(None)

Now it looks like I want it:

OK, now I'll check if it's working for other classes too (e.g. Scatter, Widget,...). If that's working I'm doing a pull request to Kivy.

相关文章