【发布时间】:2018-10-26 17:49:53
【问题描述】:
从 python 端向自定义按钮添加文本的最佳方法是什么?到目前为止,这是我的代码:
class CircularButton(ButtonBehavior, Widget):
# code inspired from:
# https://github.com/kivy/kivy/issues/4263#issuecomment-217430358
# https://stackoverflow.com/a/42886979/6924364
# https://blog.kivy.org/2014/10/updating-canvas-instructions-declared-in-python/
def __init__(self, **kwargs):
super(CircularButton,self).__init__(**kwargs)
self.add_widget(Label(text='test')) # <-- put this in the middle of the button
with self.canvas:
Color(rgba=(.5,.5,.5,.5))
self.shape = Ellipse(pos=self.pos,size=self.size)
self.bind(pos=self.update_shape, size=self.update_shape)
def update_shape(self, *args):
self.shape.pos = self.pos
self.shape.size = self.size
def collide_point(self, x, y):
return Vector(x, y).distance(self.center) <= self.width / 2
当链接到 .kv 文件中的小部件时,您会看到“文本”标签出现在 (0,0) 而不是按钮顶部。为这个自定义按钮设置文本的最佳方式是什么?
【问题讨论】: