python - 当我减小圆圈的比例时,如何删除以前的圆圈?
问题描述
我正在尝试使用 tkinter 来增加圆圈的大小。我面临一个问题,即当我减小圆圈的比例时,前一个圆圈仍然存在。因此,有什么方法可以删除之前的圈子吗?仅供参考,我使用的是 python 2.7.18
from Tkinter import *
root = Tk()
root.title('Elastic collision animation')
canvas = Canvas(root, width=500, height=500)
canvas.pack()
def redraw(a):
ball_1 = True
canvas.delete(ball_1)
canvas.create_oval(posn_x, posn_y, posn_x+ball_width+int(a),
posn_y+ball_height+int(a), fill='blue')
canvas.update()
posn_x = 50
posn_y = 50
ball_width = 10
ball_height = 10
ball_1 = canvas.create_oval(posn_x, posn_y, posn_x+ball_width,
posn_y+ball_height, fill='blue')
var = IntVar()
scale_1 = Scale(root, label='mass of ball 1', orient=HORIZONTAL, length=200,
width=15, from_=1, to= 100, variable = var, command=redraw)
scale_1.pack()
root.mainloop()
解决方案
试试这个:
from tkinter import *
root = Tk()
root.title('Elastic collision animation')
canvas = Canvas(root, width=500, height=500)
canvas.pack()
def redraw(a):
global ball_1
canvas.delete(ball_1)
ball_1 = canvas.create_oval(posn_x, posn_y, posn_x+ball_width+int(a),
posn_y+ball_height+int(a), fill='blue')
posn_x = 50
posn_y = 50
ball_width = 10
ball_height = 10
ball_1 = canvas.create_oval(posn_x, posn_y, posn_x+ball_width,
posn_y+ball_height, fill='blue')
var = IntVar()
scale_1 = Scale(root, label='mass of ball 1', orient=HORIZONTAL, length=200,
width=15, from_=1, to= 100, variable = var, command=redraw)
scale_1.pack()
root.mainloop()
我不知道你为什么有ball_1 = True
。我所做的只是删除那条线并将新创建的球 id 分配给ball_1
推荐阅读
- facebook - 如何从 url 获取 facebook 页面 id
- python - Numpy:如何从任意长度的小数组构造一个大对角线数组(矩阵)
- python - QueryBadFormed 使用 Uniprot SPARQL 端点
- reactjs - 如何使用组件内的按钮更改选项卡
- python - 根据值列表对 DataFrame 的行进行多重过滤
- python-3.x - 有没有办法解码决策树概率输出?
- android - 在 android 应用程序中进行哪些更改以使其与 Chromebook 兼容?
- python - 如何解决这种类型的错误“ValueError: setting an array element with a sequence”?
- html - 更好的对齐方式——尤其是响应式属性
- java - 从其他文件导入消息时 Protobuf 编译错误