英文:
How to change color of Ellipse when pressing a button on Kivy?
问题
我卡在如何动态更改一些椭圆元素的颜色上;我将所有生成的点放入一个列表中。然后我想要改变它们的颜色:
实际上,使用参数来改变似乎不是一个好主意
from random import random
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.graphics import Color, Ellipse, Line
points = []
class MyPaintWidget(Widget):
def __init__(self, **kwargs):
self.size= [50,50]
self.pos = [100,50]
self.r = 0
super(MyPaintWidget, self).__init__(**kwargs)
def on_touch_down(self, touch):
color = (self.r, 1, 1)
with self.canvas:
Color(*color, mode='hsv')
d = 30.
pts = Ellipse(pos=(touch.x - d / 2, touch.y - d / 2), size=(d, d))
points.append(pts)
class MyPaintApp(App):
def build(self):
parent = Widget()
self.painter = MyPaintWidget()
clearbtn = Button(text='Change Color')
clearbtn.bind(on_release=self.change_color)
parent.add_widget(self.painter)
parent.add_widget(clearbtn)
return parent
def change_color(self, obj):
print(len(points))
points[0].r = 0.2 # <= 错误!
英文:
I am stuck on how to change the color of some ellipse elements dynamically; I put all generated points into a list. Then I want to change their color:
In fact, using params to change is seems not to be a good idea for that
from random import random
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.graphics import Color, Ellipse, Line
points = []
class MyPaintWidget(Widget):
def __init__(self, **kwargs):
self.size= [50,50]
self.pos = [100,50]
self.r = 0
super(MyPaintWidget, self).__init__(**kwargs)
def on_touch_down(self, touch):
color = (self.r, 1, 1)
with self.canvas:
Color(*color, mode='hsv')
d = 30.
pts = Ellipse(pos=(touch.x - d / 2, touch.y - d / 2), size=(d, d))
points.append(pts)
class MyPaintApp(App):
def build(self):
parent = Widget()
self.painter = MyPaintWidget()
clearbtn = Button(text='Change Color')
clearbtn.bind(on_release=self.change_color)
parent.add_widget(self.painter)
parent.add_widget(clearbtn)
return parent
def change_color(self, obj):
print(len(points))
points[0].r = 0.2 <= Error !
答案1
得分: 1
您的 on_touch_down() 方法在每次触摸时向画布添加图形指令。为了更改所有“椭圆”的颜色,您需要更改每个“颜色”命令。这可以做到,但更简单的方法是清除画布,并添加新的图形指令以使用新颜色重新绘制所有“椭圆”。以下是修改后的代码版本:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.graphics import Color, Ellipse
class MyPaintWidget(Widget):
def __init__(self, **kwargs):
super(MyPaintWidget, self).__init__(**kwargs)
self.size = [50, 50]
self.pos = [100, 50]
self.d = 30
self.r = 0 # 色调
self.ellipses = [] # “椭圆”的位置列表
def redraw(self):
# 清除画布并使用新的色调重新绘制所有“椭圆”的方法
self.canvas.clear()
with self.canvas:
Color(self.r, 1, 1, mode='hsv')
for pt in self.ellipses:
Ellipse(pos=pt, size=(self.d, self.d))
def on_touch_down(self, touch):
# 只需将新位置添加到列表中并重新绘制
self.ellipses.append((touch.x - self.d / 2, touch.y - self.d / 2))
self.redraw()
class MyPaintApp(App):
def build(self):
parent = Widget()
self.painter = MyPaintWidget()
clearbtn = Button(text='更改颜色')
clearbtn.bind(on_release=self.change_color)
parent.add_widget(self.painter)
parent.add_widget(clearbtn)
return parent
def change_color(self, obj):
print(len(self.painter.ellipses))
self.painter.r = 0.2 # 更改色调
self.painter.redraw() # 并重新绘制
MyPaintApp().run()
英文:
Your on_touch_down() method is adding graphics instructions to the canvas with every touch. In order to change the color of all the Ellipses, you would need to change each of those Color commands. That could be done, but a simpler approach is to just to clear the canvas, and add new graphics instructions to redraw all the Ellipses with the new color. Here is a modified version of your code that does that:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.graphics import Color, Ellipse
class MyPaintWidget(Widget):
def __init__(self, **kwargs):
super(MyPaintWidget, self).__init__(**kwargs)
self.size= [50,50]
self.pos = [100,50]
self.d = 30
self.r = 0 # the hue
self.ellipses = [] # a list of points for the Ellipses
def redraw(self):
# method to clear the canvas and redraw all the Ellipses with new hue
self.canvas.clear()
with self.canvas:
Color(self.r, 1, 1, mode='hsv')
for pt in self.ellipses:
Ellipse(pos=pt, size=(self.d,self.d))
def on_touch_down(self, touch):
# just add a new position to the list and redraw
self.ellipses.append((touch.x - self.d / 2, touch.y - self.d / 2))
self.redraw()
class MyPaintApp(App):
def build(self):
parent = Widget()
self.painter = MyPaintWidget()
clearbtn = Button(text='Change Color')
clearbtn.bind(on_release=self.change_color)
parent.add_widget(self.painter)
parent.add_widget(clearbtn)
return parent
def change_color(self, obj):
print(len(self.painter.ellipses))
self.painter.r = 0.2 # change the hue
self.painter.redraw() # and redraw
MyPaintApp().run()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论