PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, July 18, 2022

[FIXED] How to put a moving gif image in Canvas using Class in Python 3.x

 July 18, 2022     canvas, class, gif, python, tkinter     No comments   

Issue

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()

When the code is executed the tkinter window is blank. I know to put the gif image without using class but I want to know what is the wrong in this code.

I am sorry if its too obivious or simple. I am using Python 3.5.

The code for putting gif image without class is given below.

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()

Solution

You need to split the gif and rename in numerical order and put the imagens in a folder (images) in the same dir of the script... And done. Don't use time.sleep, always use self.after on tkinter, but if you need to use time you need to call the function with self.after and in the function thats have time.sleep you need to call self.update() below.

#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()


Answered By - Radagast
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing