我有一个函数,它从文本文件中读取位置,解析它们,然后使用 coords 函数将相应的对象移动到 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-------')