我正在嘗試使用 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,我真的不明白它為什么會這樣做。無論如何,如果您有使線條移動的解決方案,而無需在每次調用函數時創建和刪除它們,請隨時告訴我。
如何將線的點移動到特定位置?
慕的地6264312
2021-09-25 16:56:47
