我正在尝试使用 tkinter 在 python 中可视化正弦波加法,并且我正在尝试在每个圆中心之间建立线,但是到目前为止我尝试过的并没有像我想象的那样工作。有没有办法解决我尝试过的问题(见代码),或者有办法只移动一条直线的一个坐标点与另一个坐标点吗?如果您运行它,您将在代码中看到,我尝试了一种方法,其中每次迭代前一行都被擦除并创建一个新行。当我运行代码时,就像我想要的那样,每个圆圈的中心之间实际上有一条线,但事实是这些线持续存在并且不会自行擦除;出于某种原因,似乎 canvas.delete(line) 不像我预期的那样工作。这是完整的代码。有趣的部分是在 'updateline' 函数中,进入 'act()' 函数。import mathimport tkinter as tk##important to know! -- the way I'm creating the circles is by setting an object, the bounds of the circle, depending on amplitude asked by user.##then the programs calculates the path of these bounds, depending on circles, amplitude, phase and frequency of the sine waves asked by the user from the tkinter GUI.##finally, the program creates and moves along this path a circle, representing visually the sine wave.top = tk.Tk()top.title('Superposition')choice = tk.Tk()choice.title('Parametres')f = tk.Frame(choice,bd=3)f.pack(side='top')g = tk.Frame(choice,bd=3)g.pack(side='bottom')tk.Label(f,text="nbre ondes:",width = 10).grid(row=0,column=0)sines = tk.Spinbox(f,from_=1,to=50,width=10,textvariable=tk.DoubleVar(value=2))sines.grid(row=0,column=1)sines.delete(0,5)sines.insert(0,2)delai = tk.Scale(g, orient='vertical', from_=100, to=1,resolution=1, length=100,label='delai')delai.grid(row=0,column=0)hauteur = tk.Scale(g, orient='vertical', from_=1100, to=100,resolution=100, length=100,label='fenetre')hauteur.grid(row=0,column=1)taillec1 = tk.Scale(g, orient='vertical', from_=3.5, to=0.1,resolution=0.1, length=100,label='taille')taillec1.grid(row=0,column=2)delai.set(20)hauteur.set(600)taillec1.set(1.5)def grilledechoix(): numberofsines = int(sines.get()) for i in f.grid_slaves(): if int(i.grid_info()["row"]) > numberofsines+2: i.grid_forget()我预计一旦 updateline 函数再次调用自身,这些行就会消失,因为 'canvas.delete(line)' 行进入了 updateline,我真的不明白它为什么会这样做。无论如何,如果您有使线条移动的解决方案,而无需在每次调用函数时创建和删除它们,请随时告诉我。
1 回答

繁花如伊
TA贡献2012条经验 获得超12个赞
如果我正确理解了问题,我相信问题出在这段代码上:
canvas.after(delai, canvas.delete, line)
canvas.create_line(oldx0+dx0, oldy0+dy0, oldx1+dx1, oldy1+dy1)
canvas.after(delai, updateline, canvas, line, robj0, cent0, robj1, cent1)
它无法将新行重新分配给line下一次调用的变量。而是尝试:
canvas.after(delai, canvas.delete, line)
line = canvas.create_line(oldx0+dx0, oldy0+dy0, oldx1+dx1, oldy1+dy1)
canvas.after(delai, updateline, canvas, line, robj0, cent0, robj1, cent1)
当我运行它时,它消除了多余的线条。如果我错过了重点,请告诉我。
添加回答
举报
0/150
提交
取消