当用户按下按钮时,目的是改变笔的颜色。颜色将变为按钮的背景颜色。画布的代码:
def on_touch_move(self,color,touch):
with self.pc.canvas:
global wtd,pencolor
Color(pencolor)
if wtd == 1:
Ellipse(pos=(touch.x,touch.y),size=(penrad,penrad))
绑定到按钮的on_press代码:
def newclr(self,instance):
global pencolor
pencolor = instance.background_color
self.on_touch_move
return pencolor
按钮事件正常工作!
答案 0 :(得分:1)
提供Color的模式(rgb,rgba)。将 颜色(pencolor) 替换为 颜色(rgba = pencolor)
有关详细信息,请参阅下面的演示。笔颜色从红色开始变为白色,即按钮的背景颜色。
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ListProperty, NumericProperty
from kivy.graphics import *
class MyWidget(Widget):
wtd = NumericProperty(1)
penrad = NumericProperty(10)
pencolor = ListProperty([1, 0, 0, 1]) # Red
def newclr(self, instance):
print("Before Change@newclr: pencolor=", self.pencolor)
self.pencolor = instance.background_color
print("After Change@newclr: pencolor=", self.pencolor)
def on_touch_move(self, touch):
print("on_touch_move: touch=", touch)
print("on_touch_move: pencolor=", self.pencolor)
with self.canvas:
Color(rgba=self.pencolor)
if self.wtd == 1:
Ellipse(pos=(touch.x, touch.y), size=(self.penrad, self.penrad))
class TestApp(App):
title = "Kivy - Change Pen Colour"
def build(self):
return MyWidget()
if __name__ == "__main__":
TestApp().run()
#:kivy 1.10.0
<CustomButton>:
<MyWidget>:
canvas.before:
Color:
rgba: 0, 0, 1, 1 # Blue
Rectangle:
pos: self.pos
size: self.size
Button:
text: "Change Pen Color"
size: 150, 100
size_hint: None, None
on_press: root.newclr(self)