我有一個函數可以從文本文件讀取位置,解析它們,然後使用coords函數將各個對象移動到tkinter畫布上列出的位置。正在從文件中讀取數據並正確解析數據,但由於某些原因,coords函數僅將對象移動到循環上次迭代中文件中列出的最後一個位置。Python Tkinter coords函數不會在循環內移動畫布對象
我是否需要在循環的每次迭代後以某種方式更新畫布?謝謝!
這裏是我的代碼:
def playback():
fptr = tkFileDialog.askopenfilename()
filename = open(fptr,"rU")
if not filename:
return
stat.set('REPLAY IN PROGRESS')
gamestatus[0] = 2
for line in filename:
line = line.strip()
#Example line from input file: 'B:#,#,#,#|L:#,#,#,#|R:#,#,#,#'
line = line.split('|')
B_loc = line[0].split(':')[1].split(',')
L_loc = line[1].split(':')[1].split(',')
R_loc = line[2].split(':')[1].split(',')
#Converting strings to ints and lists to tuples to simplify code below
B_tup=(int(B_loc[0]),int(B_loc[1]),int(B_loc[2]),int(B_loc[3]))
L_tup=(int(L_loc[0]),int(L_loc[1]),int(L_loc[2]),int(L_loc[3]))
R_tup=(int(R_loc[0]),int(R_loc[1]),int(R_loc[2]),int(R_loc[3]))
#Moving objects to locations from input file
playingField.coords(pongball.ball,B_tup)
playingField.coords(leftpaddle.paddle,L_tup)
playingField.coords(rightpaddle.paddle,R_tup)
time.sleep(.02)
filename.close()
gamestatus[0] = 0
stat.set('-------Pong-------')
您的壓痕看起來有點小;緊接在「for」後面的行與「for」的縮進位置相同 – 2013-04-25 17:19:55
這只是我複製時的一個問題。原始代碼中的縮進是正確的。我編輯了這篇文章以反映這一點。謝謝! – carpenma 2013-04-25 18:38:07