繁体   English   中英

python tkinter中的update_idletasks困难

[英]update_idletasks difficulty in python tkinter

我在调用.update_idletasks().update_idletasks()遇到麻烦(在.set_up()类方法的末尾)。

如果我使用self.canv.update_idletasks( self ) ,则画布会绘制我想要的对象,但会给出错误

TypeError: update_idletasks() takes exactly 1 argument (2 given)

如果我使用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()

python中的self实际上是语法糖。

换句话说,当您说出self.animate() ,python会将其转换为Animation.animate(self) 这就是为什么要使用“自我”参数定义类方法的原因-它被隐式传递。

self.canv.update_idletasks(self)正在评估类似Canvas.update_idletasks(self.canv, self)

通过查看一些类似的代码,我找到了一个解决方案。 self.canv.get_tk_widgets().update_idletasks( )可以解决问题,以为我不太确定为什么self.canv.get_tk_widgets().update_idletasks( ,如果有人可以解释或指出我需要阅读一些内容。 谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM