【问题标题】:How to put a moving gif image in Canvas using Class in Python 3.x如何使用 Python 3.x 中的 Class 在 Canvas 中放置动态 gif 图像
【发布时间】:2016-01-27 08:06:02
【问题描述】:
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&quo­t;", 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()

【问题讨论】:

  • "我知道不使用类就放 gif 图像"。你是说你有一个不使用类的工作版本吗?你也能证明吗?我很难确定这段代码要做什么。
  • 当您说“运动图像”时,您的意思是希望图像在屏幕上移动,还是“运动图像”的意思是“像电影一样播放一系列图像”?
  • @Kevin 是的,我今天会发给你它的链接
  • @Bryan Oakley 我的意思是像电影一样播放一系列图像
  • @Kevin m.youtube.com/… 检查第一个窗口的代码。在那里你会发现如何放置动态 gif 图像。否则,如果您愿意,我将向您发送仅用于放置 gif 图像的代码

标签: python class canvas tkinter gif


【解决方案1】:

您需要拆分 gif 并按数字顺序重命名,并将图像放在脚本相同目录中的文件夹(图像)中......然后完成。不要使用 time.sleep,总是在 tkinter 上使用 self.after,但是如果你需要使用 time,你需要使用 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()

【讨论】:

  • 感谢您的回答。这就是我所做的,在 image 函数中,所有的 gif 帧都附加到 giflist 中,然后在 Run 函数中,gif 图像和时间被打包到 for 循环中,延迟为 0.1s。
  • 天哪,我看到了错误...是time.sleep,我会改变答案
  • 非常感谢。第二个错误是 init 我使用 _ 而不是 __
猜你喜欢
  • 2018-06-22
  • 2021-09-25
  • 2010-11-17
  • 1970-01-01
  • 1970-01-01
  • 2012-08-24
  • 1970-01-01
  • 1970-01-01
  • 2012-06-03
相关资源
最近更新 更多