gpt4 book ai didi

Python:在线程运行时使用 TKinter 制作 gif 动画

转载 作者:行者123 更新时间:2023-12-05 05:28:22 28 4
gpt4 key购买 nike

因此,我相信您可以从我相当长的主题中看出我要重申的问题。当后台线程正在 TKinter 中运行计算时,我正在尝试制作 gif 动画。为了简单起见,我正在使用这个 gif:http://i.imgur.com/4Y9UJ.gif

gif 动画当前正在运行,您可以在运行脚本时看到这一点。但是只要按下生成按钮并启动线程,gif 就会暂停直到完成。我正在使用 time.sleep() 来模拟我将在后台进行的大量计算(尽管我不确定这是否是真正导致问题的原因)。

我敢肯定这与不了解动画的确切运作方式有关。有什么建议吗?

代码如下:

from Tkinter import *
import tkMessageBox
import time
import os
import threading
from PIL import Image, ImageTk

class Gif(Label):
def __init__(self, master, filename):
evanGif = Image.open(filename)
gifSeq = []
try:
while 1:
gifSeq.append(evanGif.copy())
evanGif.seek(len(gifSeq)) # skip to next frame
except EOFError:
pass # we're done
try:
#Special case for the evangalion gif
if evanGif.info['duration'] == 0:
self.delay = 100
else:
self.delay = evanGif.info['duration']
except KeyError:
self.delay = 100
gifFirst =gifSeq[0].convert('RGBA')
self.gifFrames = [ImageTk.PhotoImage(gifFirst)]

Label.__init__(self, master, image=self.gifFrames[0])

temp =gifSeq[0]
for image in gifSeq[1:]:
temp.paste(image)
frame = temp.convert('RGBA')
self.gifFrames.append(ImageTk.PhotoImage(frame))

self.gifIdx = 0
self.cancel = self.after(self.delay, self.play)

def play(self):
self.config(image=self.gifFrames[self.gifIdx])
self.gifIdx += 1
if self.gifIdx == len(self.gifFrames):
self.gifIdx = 0
self.cancel = self.after(self.delay, self.play)

class App:
generating = False
def __init__(self, master):
self.master=master

#Initializing frames
self.buttonFrame = Frame(master, background='light gray')
self.loadingFrame = Frame(master, background='light gray')
self.loadingFrame.grid(row=0)
self.buttonFrame.grid(row=1)
self.anim = Gif(self.loadingFrame, '4Y9UJ.gif').pack()
self.update_Thread = threading.Thread(target=time.sleep, args=(5,))
self.buttonSetup()

def buttonSetup(self):
#ALL THE BUTTONS
self.generateBt = Button(self.buttonFrame, text="Generate!", command=self.generate, background='light gray', highlightbackground='light gray')
self.generateBt.pack(side=LEFT)
self.quitBt = Button(self.buttonFrame, text="Quit!", fg="red", command=self.buttonFrame.quit, background='light gray', highlightbackground='light gray')
self.quitBt.pack(side=LEFT)

def generate(self):
self.hideForGen()
self.update_Thread.start()
while(self.update_Thread.isAlive()):
self.master.update_idletasks()
self.reset()
self.master.update_idletasks()
tkMessageBox.showinfo("Complete", "Report generation completed!")

def hideForGen(self):
self.buttonFrame.grid_forget()

def reset(self):
self.buttonFrame.grid(row=1)

root = Tk()
root.title('Test')
root.configure(background='light gray')
app = App(root)
root.mainloop()

最佳答案

问题出在您的 generate() 方法中。while 循环是不必要的。

请记住,您无法重新启动一个线程。如果您想多次使用生成按钮,则每次都需要创建一个新线程。

class App:
generating = False
def __init__(self, master):
self.master=master

#Initializing frames
self.buttonFrame = Frame(master, background='light gray')
self.loadingFrame = Frame(master, background='light gray')
self.loadingFrame.grid(row=0)
self.buttonFrame.grid(row=1)
self.anim = Gif(self.loadingFrame, '4Y9UJ.gif')
self.anim.pack()
## self.update_Thread = threading.Thread(target=time.sleep, args=(5,))

self.buttonSetup()

def buttonSetup(self):
#ALL THE BUTTONS
self.generateBt = Button(self.buttonFrame, text="Generate!", command=self.generate, background='light gray', highlightbackground='light gray')
self.generateBt.pack(side=LEFT)
self.quitBt = Button(self.buttonFrame, text="Quit!", fg="red", command=self.buttonFrame.quit, background='light gray', highlightbackground='light gray')
self.quitBt.pack(side=LEFT)

def wait_generate(self):
if self.update_Thread.isAlive():
self.master.after(500, self.wait_generate)
else:
tkMessageBox.showinfo("Complete", "Report generation completed!")
self.reset()

def generate(self):
self.hideForGen()
self.update_Thread = threading.Thread(target=time.sleep, args=(5,))
self.update_Thread.start()
## while(self.update_Thread.isAlive()):
## self.master.update_idletasks()
## self.reset()
## self.master.update_idletasks()
## tkMessageBox.showinfo("Complete", "Report generation completed!")
self.wait_generate()

def hideForGen(self):
self.buttonFrame.grid_forget()

def reset(self):
self.buttonFrame.grid(row=1)

关于Python:在线程运行时使用 TKinter 制作 gif 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11497204/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com