如何在 Canvas 中使用 Class 在 Python 3.x 中放置动态 gif 图像
How to put a moving gif image in Canvas using Class in Python 3.x
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 window 为空白。我知道在不使用 class 的情况下放置 gif 图片,但我想知道这段代码有什么问题。
如果太明显或太简单,我很抱歉。我正在使用 Python 3.5.
不带class放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()
您需要拆分 gif 并按数字顺序重命名,然后将图像放在脚本同一目录下的文件夹 (images) 中...完成。不要使用 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()
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 window 为空白。我知道在不使用 class 的情况下放置 gif 图片,但我想知道这段代码有什么问题。
如果太明显或太简单,我很抱歉。我正在使用 Python 3.5.
不带class放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()
您需要拆分 gif 并按数字顺序重命名,然后将图像放在脚本同一目录下的文件夹 (images) 中...完成。不要使用 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()