【发布时间】:2022-06-17 21:14:33
【问题描述】:
我搜索了整个网络和 StackOverflow 网站,但没有找到任何适合我的问题的答案。
我有一个用于创建 gif 对象的 Python 类:
import tkinter as tk
from PIL import ImageTk, Image
from itertools import count
class ImageLabel(tk.Label):
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="")
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)
可以如下使用(假设我们之前定义了一个框架):
gif = ImageLabel( frame )
gif.load( "path/to/spinner.gif" )
gif.place( anchor="center", relx= 0.7, rely=0.5 )
我希望能够在同一帧中与另一个命令并行运行这个创建的 gif。例如:假设我正在单击一个执行长时间操作的按钮,在这种情况下,我希望在其中显示一个 gif,它并行运行并在进程完成后被销毁。
你能帮帮我吗?谢谢。
【问题讨论】:
-
在一个线程中运行长操作。
-
@acw1668 你能举个例子吗?谢谢。
-
学习threading模块的官方文档。
-
@acw1668 我知道如何使用线程。事实是长操作在按钮命令中。我该如何解决?
-
只需创建另一个函数以在线程中启动原始函数,然后将
command选项设置为新函数。
标签: python python-3.x tkinter graphics