我正在尝试对gif动画进行线程处理,将其放入标签Tkinter小部件和进度条中,以便它们在执行脚本的同时运行。之后,我想使用time.sleep(10)
使其同时运行10秒钟,然后让进度条停止使用progressbar.stop()
。我的代码如下:
import tkinter
from tkinter import ttk
from tkinter import *
import time
from PIL import Image, ImageTk
from itertools import count
import threading
def main_fun():
global progressbar, lbl
window = tkinter.Tk()
window.geometry("390x600") # Width x Height
# progress bar
progressbar = ttk.Progressbar(None) # ttk is method inside tkinter
progressbar.config(orient="horizontal",
mode='indeterminate', maximum=100, value=0)
progressbar.pack(side=TOP)
# gif image class
class ImageLabel(tkinter.Label):
"""a label that displays images, and plays them if they are gifs"""
def load(self, im):
if isinstance(im, str):
im = Image.open(im)
self.loc = 0
self.frames = []
try:
for i in count(1):
self.frames.append(ImageTk.PhotoImage(im.copy()))
im.seek(i)
except EOFError:
pass
try:
self.delay = im.info['duration']
except:
self.delay = 100
if len(self.frames) == 1:
self.config(image=self.frames[0])
else:
self.next_frame()
def unload(self):
self.config(image=None)
self.frames = None
def next_frame(self):
if self.frames:
self.loc += 1
self.loc %= len(self.frames)
self.config(image=self.frames[self.loc])
self.after(self.delay, self.next_frame)
lbl = ImageLabel(window)
lbl.pack(anchor="center")
lbl.load(
'C:/Users/*****/test.gif')
# thread the label with the gif
t = threading.Thread(target=lbl, args=(None,))
t.start()
window.mainloop()
main_fun()
progressbar.start(8) # 8 is for speed of bounce
t = threading.Thread(target=progressbar, args=(None,)
) # thread the progressbar
#t.daemon = True
t.start()
time.sleep(10) # 10 second delay, then progressbar must stop
progressbar.stop()
我对线程不熟悉,所以我不明白我在做什么错。我得到了错误:
TypeError:“ ImageLabel”对象不可调用
TypeError:“进度条”对象不可调用
请协助。
答案 0 :(得分:1)
您可以使用answer given here在另一个线程上实现进度条。另外,您所做的错误是您的progressbar
不是callable object, nor does it override the run()
method。