亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將線的點移動到特定位置?

如何將線的點移動到特定位置?

慕的地6264312 2021-09-25 16:56:47
我正在嘗試使用 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)

當我運行它時,它消除了多余的線條。如果我錯過了重點,請告訴我。


查看完整回答
反對 回復 2021-09-25
  • 1 回答
  • 0 關注
  • 256 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號