Python Tkinter中update_idletasks的难点
我在调用 .update_idletasks()
来重新绘制我的画布时遇到了问题(在 .set_up()
这个类方法的最后)。
如果我使用 self.canv.update_idletasks( self )
,画布会绘制出我想要的对象,但会出现错误:
TypeError: update_idletasks() 需要一个参数(给了两个)
如果我使用 self.canv.update_idletasks()
,画布保持空白,但没有产生任何错误。
from Tkinter import *
class Animation(Frame):
def __init__(self,parent=None):
Frame.__init__(self,parent)
self.pack()
self.generation = 1
self.epoch = 1
self.set_up()
def set_up(self):
greeting_text = 'Generation ' + str(self.generation) + ', Epoch ' + str(self.epoch)
header = Label(self,text=greeting_text)
header.pack()
anframe = Frame(self,relief=SUNKEN)
anframe.pack(side='left',padx=10,pady=10)
self.canv = Canvas(anframe,width=800, height=800, bg='white') # 0,0 is top left corner
self.canv.pack(expand=YES, fill=BOTH,side='left')
buframe = Frame(self,relief=RAISED)
buframe.pack(side='right',fill=X,padx=10,pady=5)
start = Button(buframe,text='START',width=10,command=self.animate)
start.pack(expand=YES,fill=BOTH,side='top')
back = Button(buframe,text='Back',width=10)
back.pack(expand=YES,fill=BOTH,side='bottom')
nextgo= Button(buframe,text='Next',width=10)
nextgo.pack(expand=YES,fill=BOTH,side='bottom')
path = 'C:/Desktop/LearnNet/' + str(self.generation) + '_' + str(self.epoch) + '.txt'
data = open(path,'r')
lines = data.readlines()
food,moves = [],[]
for item in lines:
if item.strip() == 'f':
dat_type = 1
continue
elif item.strip() == 'a':
dat_type = 2
continue
if dat_type == 1:
temp = item.strip()
temp = item.split()
food.append([int(temp[0]),int(temp[1])])
if dat_type == 2:
temp = item.strip()
temp = item.split()
moves.append([int(temp[0]),int(temp[1]),int(temp[2])])
photos = []
for i in xrange(len(food)):
temp=PhotoImage(file='C:/Desktop/LearnNet/Food.gif')
photos.append(temp)
self.canv.create_image(food[i][0]*20,food[i][1]*20,image=photos[i],anchor=NW)
start_pos = moves[0]
picpath = self.orientation(start_pos[2])
animal = PhotoImage(file=picpath)
self.canv.create_image(moves[0][0]*20,moves[0][1]*20,image=animal,anchor=NW)
self.canv.update_idletasks(self)
def animate(self):
return 1
def orientation(self,angle):
if angle == 0:
picpath = 'C:/Desktop/LearnNet/PEast.gif'
elif angle == 90:
picpath = 'C:/Desktop/LearnNet/PNorth.gif'
elif angle == 180:
picpath = 'C:/Desktop/LearnNet/PWest.gif'
else:
picpath = 'C:/Desktop/LearnNet/PSouth.gif'
return picpath
if __name__ == '__main__': Animation().mainloop()
2 个回答
1
我通过查看一些类似的代码找到了一个解决办法。self.canv.get_tk_widgets().update_idletasks(
这个方法可以解决问题,不过我不太明白为什么它有效。如果有人能解释一下或者推荐一些相关的资料,那就太好了。谢谢!
4
在Python中,self
其实是一种语法上的简化。
换句话说,当你写self.animate()
的时候,Python会把它理解成Animation.animate(self)
。所以你在定义类的方法时需要加上这个“self”参数——它是自动传递的。
self.canv.update_idletasks(self)
的意思其实是Canvas.update_idletasks(self.canv, self)
。