Kivy自定义按钮on_press更改画布颜色

时间:2018-10-26 19:26:04

标签: python kivy

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

test:
${INFO} "Pulling latest images..."
@ docker-compose -p $(DEV_PROJECT) -f $(DEV_COMPOSE_FILE) pull
${INFO} "Building images..."
@ docker-compose -p $(DEV_PROJECT) -f $(DEV_COMPOSE_FILE) build --pull test
@ docker-compose -p $(DEV_PROJECT) -f $(DEV_COMPOSE_FILE) build cache
${INFO} "Ensuring database is ready..."
@ docker-compose -p $(DEV_PROJECT) -f $(DEV_COMPOSE_FILE) run --rm agent
${INFO} "Running tests..."
@ docker-compose -p $(DEV_PROJECT) -f $(DEV_COMPOSE_FILE) up test
# @ cd $$(docker-compose -p $(DEV_PROJECT) -f $(DEV_COMPOSE_FILE) ps -q test) 
# @ cp *.xml ./reports
@ docker cp $$(docker-compose -p $(DEV_PROJECT) -f $(DEV_COMPOSE_FILE) ps -q test):/reports/. reports
${CHECK} $(DEV_PROJECT) $(DEV_COMPOSE_FILE) test
${INFO} "Testing complete"

2 个答案:

答案 0 :(得分:1)

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

const regex = /^(?:.*?\.){2}(.*)$/gm;
const str = `buildA.equipA.point
buildA.equipA.another.point
buildA.equipA.yet.another.point`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    console.log(m[1]);
}

尽管我更喜欢将.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.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())

答案 1 :(得分:0)

也许可以帮助某人解决此问题:

.py

class PositionButton(Button):
    b_color = ListProperty()

.kv

<PositionButton>:
    background_color: [0,0,0,0]
    b_color: [0, 0, 1, .3] if self.state == 'normal' else [0, 0, 1, .5]
    canvas.before:
        Color:
            rgba: self.b_color