【问题标题】:How create a circular button with kivy?如何用kivy创建一个圆形按钮?
【发布时间】:2021-08-25 09:45:02
【问题描述】:

我需要一个没有矩形的完美圆形按钮。我已经制作了,但是当我在圆(角)之外单击它时,它也被按下,它的行为就好像它是一个矩形,在其中绘制一个圆圈是一个问题。请帮忙!

class Dom(BoxLayout):
    pass

class CBB(ButtonBehavior, Label):
    pass

class CBApp(App):
    pass

CBApp().run()

Kivy file---CB.kv
Dom:
<CBB>:
    canvas:
        Color:
            rgba: (1, 0, 1, 0.1) if self.state == 'down' else (1,1,0,0.1)
        Ellipse:
            id:el
            pos: self.center[0]- 100,self.center[1]- 100
            size: 200, 200
            angle_start: 0
            angle_end: 360

        
<Dom>:
    BoxLayout:
        orientation:"vertical"
        CBB:
            text:"Hello World"
            on_press:print("Hello World")

【问题讨论】:

标签: kivy kivy-language


【解决方案1】:

您可以将on_touch_down() 方法添加到您的CBB 中,以检查按钮按下是否与圆圈一起发生:

class CBB(ButtonBehavior, Label):
    def on_touch_down(self, touch):
        dist = sqrt( pow(touch.pos[0] - self.center[0], 2) + pow(touch.pos[1] - self.center[1], 2) )
        if dist <= 100:  # radius of the circle
            return super(CBB, self).on_touch_down(touch)
        else:
            return False

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-23
    • 2019-12-24
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 2017-05-23
    • 2015-07-27
    • 1970-01-01
    相关资源
    最近更新 更多