from tkinter import *
from time import *
import os
class gui(Canvas):
def _init_(self,parent):
Canvas.__init__(self,parent)
self.pack(fill=BOTH)
root.protocol("WM_DELETE_WINDOW"", self.close)
def image(self):
self.giflist=[]
self.imagelist=[All frames of the gif image]
for image in self.imagelist:
self.photo=PhotoImage(file=image)
self.giflist.append(self.photo)
self.label=Label(self,fg="black",font=("arial",20,"italic"))
self.label1=Label(self,bd=0)
def Run(self):
for gif in self.giflist:
self.delete(ALL)
self.label.config(text=asctime)
self.label.text=asctime()
self.label.pack()
self.label1.config(image=gif)
self.label1.image=gif
self.label1.pack()
self.update()
sleep(0.1)
def close(self):
os._exit(0)
root=Tk()
frame=Canvas(root)
g=gui(frame)
g.image()
while True:
g.Run()
root.mainloop()
执行代码时,tkinter窗口为空白。我知道在不使用类的情况下放置gif图像但是我想知道这段代码中的错误。
如果它过于简单或简单,我很抱歉。我使用的是Python 3.5。
下面给出了不带类的gif图像的代码。
from tkinter import *
from time import *
root=Tk()
frame=Canvas(root)
frame.pack()
imagelist=[All frames of gif image]
giflist=[]
for image in imagelist:
photo=PhotoImage(file=image)
giflist.append(photo)
l=Label(frame,bd=0)
while True:
for gif in giflist:
l.config(image=gif)
l.image=gif
l.pack()
sleep(0.1)
frame.update()
root.mainloop()
答案 0 :(得分:0)
你需要按照数字顺序拆分gif并重命名,并将图像放在脚本的同一目录中的文件夹(图像)中......并完成。不要使用time.sleep,总是在tkinter上使用self.after,但是如果你需要使用时间,你需要使用self.after调用该函数,并且在具有time.sleep的函数中你需要调用self.update( )下面。
#IMPORTS
try:
import tkinter as tk
import os
from PIL import Image, ImageTk
print("[OK] Imports")
except ValueError as error:
print(error)
quit()
imagesFiles = []
atualFrame = 0
#GET IMAGES FROM FOLDER
def getImageList():
global imagesFiles
for files in os.listdir("./images"):
imagesFiles.append(files.split(".")[0])
imagesFiles = sorted(imagesFiles, key = int)
#CHANGE FRAMES
def changeFrames(self):
global atualFrame
self.img = ImageTk.PhotoImage(Image.open("./images/" + imagesFiles[atualFrame] + ".gif"))
self.label["image"] = self.img
if atualFrame == len(imagesFiles) - 1:
atualFrame = 0
atualFrame += 1
self.after(100, lambda: changeFrames(self))
class mainPage(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
getImageList()
self.label = tk.Label(self)
self.label.pack()
changeFrames(self)
app = mainPage()
#app.geometry("600x600")
#app.config(cursor = "none")
#SET FULLSCREEN
#app.attributes("-fullscreen", True)
app.mainloop()