TypeError:'int'和'Label'实例之间不支持'>'

时间:2019-04-29 20:26:29

标签: python python-3.x tkinter

所以我有一个动画的gif并出现类型错误

Traceback (most recent call last):
  File "Untitled.py", line 36, in <module>
    animate(last_frame,label,framelist,0)  # Start animation
  File "Untitled.py", line 4, in animate
    if frame_number > last_frame:
TypeError: '>' not supported between instances of 'int' and 'Label'

当def位于主循环中时它起作用。但是我需要有动画循环。所以我想知道我是否需要做一些转换工作

import tkinter as tk
def animate(frame_number,last_frame,label,framelist):
    #if the frame number is bigger than the last frame
    if frame_number > last_frame:
        frame_number = 0
        #how long each frame should last
    label.config(image=framelist[frame_number]) 
    window.after(1500, animate, frame_number+1)
#make mainwindow
window = tk.Tk()
# List to hold all the frames
framelist = [] 
# Frame index
frame_index = 0    

while True:
    try:
        # Read a frame from GIF file
        part = 'gif -index {}'.format(frame_index,fr)
        frame = tk.PhotoImage(file='giphy.gif', format=part)
    except:
        # Save index for last frame
        last_frame = frame_index - 1 
         # Will break when GIF index is reached
        break              
    framelist.append(frame)
     # Next frame index
    frame_index += 1       


#put the gif into a label
label = tk.Label(window, bg='black')
label.grid(column = 1, row = 7)

animate(last_frame,label,framelist,0)  # Start animation
#window size
window.geometry("600x600")

#end of page
window.mainloop()

预先感谢

1 个答案:

答案 0 :(得分:0)

似乎您将参数以错误的顺序传递给函数。

更改此行:

animate(last_frame, label, framelist, 0)

此行:

animate(0, last_frame, label, framelist)

仔细查看您的代码,我注意到如果您没有有效的gif,那么它将在第一个循环中中断。如果您确实有一个有效的gif文件,那么您的after()语句就会出现问题。

将您的after()语句更改为:

window.after(100, animate, frame_number+1, last_frame, label, framelist)

这是我对此进行的更改以使其起作用。请记住,您必须将有效的gif定位为目标,否则它将一开始就中断。

import tkinter as tk


window = tk.Tk()
window.geometry("600x600")
framelist = []
frame_index = 0


def animate(frame_number, last_frame, label, framelist):
    if frame_number > last_frame:
        frame_number = 0
    label.config(image=framelist[frame_number])
    window.after(100, animate, frame_number+1, last_frame, label, framelist)

while True:
    try:
        part = 'gif -index {}'.format(frame_index)
        frame = tk.PhotoImage(file='./Images/giphy.gif', format=part)
    except:
        last_frame = frame_index - 1
        break
    framelist.append(frame)
    frame_index += 1

label = tk.Label(window, bg='black')
label.grid(column=1, row=7)

animate(0, last_frame, label, framelist)

window.mainloop()