问题描述
限时送ChatGPT账号..我正在尝试模拟一个美国交通灯,在一个矩形上有 3 个圆圈,所有圆圈都绘制在一组 Canvas 上.模拟应该通过按以下顺序每 2 秒更改显示的灯光来镜像动画":绿色 > 黄色 > 红色 > 绿色,等等.
I'm trying to simulate an American traffic light, with 3 circles on a rectangle, all drawn on a set Canvas. The simulation is supposed to mirror "animation" by changing which light is displayed every 2 seconds in the following order: green > yellow > red > green, etc forever.
我能想到的唯一方法是使用 canvas.move()、canvas.after()、canvas.update() 模式移动一个填充的椭圆对象,一次叠加一个未填充的圆.我已经掌握了以正确的速度和正确的顺序移动一个圆圈的逻辑.问题是,我只是实例化了一个充满绿色"的圆圈,但我无法使用这种方法将其更改为黄色"或红色".必须每 2 秒使用 canvas.delete("filled") 并在不同的地方重新绘制它,这似乎很愚蠢,因为对于这样一个简单的程序来说,这需要做很多事情.
The only way I can think of to do this is by using a canvas.move(), canvas.after(), canvas.update() pattern to move a filled oval object to superimpose one unfilled circle at a time. I've gotten the logic down to move a circle at the proper speed and in the correct order. The thing is, I just instantiate a circle filled with "green", but I can't change it to be "yellow" or "red" using this method. It seems silly to have to canvas.delete("filled") and redraw it in a new place with a different fill every 2 seconds, because that's a lot to do for such a simple program.
问题 1:有没有办法可以使用某种方法或其他方式随意更改已填充 Canvas 对象的 fill
选项?
Question 1: Is there a way I can just alter the fill
option for my filled Canvas object at will, using some method or other means?
问题 2:我是否错误地处理了这种情况?有没有更好的方法来模拟这个?
Question 2: Am I approaching this scenario incorrectly? Is there a better way to simulate this?
推荐答案
是的,您应该能够通过 config()
.
Yes you should be able to change settings of the canvas with config()
.
同样,使用 itemconfig()
更改画布上的项目.这确实需要您保存项目的句柄或标记它们.
Likewise, use itemconfig()
to change items on the canvas. This does require that you save a handle to the item or tag them.
基于 tkinterbook 的示例:
Example based on tkinterbook:
item = canvas.create_line(xy, fill="red")
canvas.coords(item, new_xy) # change coordinates
canvas.itemconfig(item, fill="blue") # change color
这篇关于创建后可以更改 Canvas 对象的属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论