gpt4 book ai didi

python - Kivy 自定义按钮 on_press 改变 Canvas 颜色

转载 作者:太空宇宙 更新时间:2023-11-04 04:28:59 25 4
gpt4 key购买 nike

关于我的最后一个问题,我一直在查看文档和在线示例,了解如何在按下时更改自定义按钮的 Canvas 颜色。这是我所拥有的;单击时没有任何变化:

class CircularButton(ButtonBehavior, Label):

# 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)

with self.canvas.before:
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 on_press(self, *args): #<--- what am I doing wrong here?
with self.canvas:
Color(rgba=(0,0,0,0))

def collide_point(self, x, y):
return Vector(x, y).distance(self.center) <= self.width / 2

最佳答案

您必须存储和重复使用 Color 指令并在 Canvas 添加指令时更改颜色,在您的情况下,您正在添加一个新的 Color 指令,该指令不适用于 Rectangle 或 Ellipse 等其他元素,因此您看不到效果.

from kivy.base import runTouchApp
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.label import Label
from kivy.vector import Vector
from kivy.graphics import Color, Ellipse
from kivy.properties import ListProperty


class CircularButton(ButtonBehavior, Label):
background_color = ListProperty((0.5,.5,.5,.5))

def __init__(self, **kwargs):
super(CircularButton,self).__init__(**kwargs)
self.draw()
self.text='test'

def update_shape(self, *args):
self.shape.pos = self.pos
self.shape.size = self.size

def on_background_color(self, *args):
self.shape_color.rgba = self.background_color

def draw(self, *args):
with self.canvas.before:
self.shape_color = Color(rgba=(0.5,.5,.5,.5))
self.shape = Ellipse(pos=self.pos,size=self.size)
self.bind(pos=self.update_shape, size=self.update_shape)

def on_press(self, *args):
self.background_color= (1, 0, 0, 1)

def on_release(self, *arg):
self.background_color = (0.5,.5,.5,.5)

def collide_point(self, x, y):
return Vector(x, y).distance(self.center) <= self.width / 2

if __name__ == '__main__':
runTouchApp(CircularButton())

虽然我更喜欢将 .kv 和 .py 结合起来,但利用 kv 语言是声明式的,因此连接很简单:

from kivy.base import runTouchApp
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.label import Label
from kivy.vector import Vector
from kivy.lang import Builder


Builder.load_string('''
<CircularButton>
background_color: 0.5,.5,.5,.5
canvas.before:
Color:
rgba: self.background_color
Ellipse:
pos: self.pos
size: self.size
''')


class CircularButton(ButtonBehavior, Label):
def __init__(self, **kwargs):
super(CircularButton, self).__init__(**kwargs)
self.text = "test"

def on_press(self, *args):
self.background_color = (1, 0, 0, 1)

def collide_point(self, x, y):
return Vector(x, y).distance(self.center) <= self.width / 2

if __name__ == '__main__':
runTouchApp(CircularButton())

关于python - Kivy 自定义按钮 on_press 改变 Canvas 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53015154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com