Search code examples
pythonpython-3.xwindowsdebuggingtkinter

problem with insert GIF in python(TKinter)?


How to make it so that the gif was not in the main window (menu, greeting), but in the final window . I was able to insert a gif in the beginning of the program, but in the final window did not work, how to make the final with a gif, if I have this code :

import tkinter as tk
from PIL import Image, ImageTk

def open_second_window():
    second_window = tk.Toplevel(root)
    second_window.title("SECOND")
    second_window.state('zoomed')
 
    image_path = "1.jpg"
    image = Image.open(image_path)
    photo = ImageTk.PhotoImage(image)


    label = tk.Label(second_window, image=photo)
    label.image = photo 
    label.pack(fill=tk.BOTH, expand=True)
    label.place(x=770, y=0)
 
    button = tk.Button(second_window,height=4, width=12, font='Times 31', text="ONE", command=lambda: open_four_window(second_window))

    button.place(x=308, y=82, anchor='ne')
    button1 = tk.Button(second_window,height=4, width=12, font='Times 31', wraplength=289, text="TWO", command=open_five_window)
    button1.place(x=688, y=82, anchor='ne')
    button = tk.Button(second_window,height=4, width=12, font='Times 31', text="THREE", command=open_threee_window)
    button.place(x=308, y=352, anchor='ne')
    button1 = tk.Button(second_window,height=4, width=12, font='Times 31', text="FOUR", command=open_threetenfive_window)
    button1.place(x=688, y=352, anchor='ne')


    tk.Label(second_window, font='Times 30', text="TEXT", fg="red").place(x=150, y=30)


    third_window.grab_set()
def open_four_window(second_window):

    second_window.destroy()

    four_window = tk.Toplevel(root)
    four_window.title("THREE")
    four_window.state('zoomed')

    image_path = "2.jpg"
    image = Image.open(image_path)
    photo = ImageTk.PhotoImage(image)

    label = tk.Label(four_window, image=photo)
    label.image = photo  
    label.pack(fill=tk.BOTH, expand=True)
    label.place(x=0, y=0)


    button = tk.Button(four_window,height=1, width=33, font='Times 31', text="START", command=lambda: open_second_window())
    button.place(x=768, y=720, anchor='ne')
    button1 = tk.Button(four_window,height=1, width=33, font='Times 31', text="EXIT", command=animation)
    button1.place(x=1536, y=720, anchor='ne')



    third_window.grab_set()


...

def open_theend_window():
    # Создаем дочернее окно
    theend_window = tk.Toplevel(root)
    theend_window.title("Второе окно")
    theend_window.state('zoomed')

    photo = tkinter.PhotoImage(file = 'road-sign-roadtrip.gif')
    lbl = tkinter.Label(theend_window ,image = photo)
    lbl.image = photo #keeping a reference in this line
    lbl.grid(row=0, column=0)

    # Создаем метку для отображения изображения
    
     # Сохраняем ссылку на изображение



    # Создаем кнопку 
    button = tk.Button(theend_window,height=1, width=33, font='Times 31', text="В начало", command=lambda: open_second_window())
    button.place(x=768, y=720, anchor='ne')
    button1 = tk.Button(theend_window,height=1, width=33, font='Times 31', text="Выход", command=Close)
    button1.place(x=1536, y=720, anchor='ne')



    # Устанавливаем режим grab_set()
    third_window.grab_set()






5# Создаем главное окно
root = tk.Tk()
root.title("Главное окно")
root.state('zoomed')


# Загружаем изображение 001.jpg
image_path = "40.jpg"
image = Image.open(image_path)
photo = ImageTk.PhotoImage(image)
image.thumbnail((1, 1))

# Создаем метку для отображения изображения
label = tk.Label(root, image=photo)
label.pack(fill=tk.BOTH, expand=True)
label.place(x=-3, y=0)

def Close(): 
    root.destroy() 

# Создаем кнопку "Далее"
button = tk.Button(root,height=1, width=33, font='Times 31', text="NEXT", command=lambda: open_second_window())
button.place(x=768, y=720, anchor='ne')
button1 = tk.Button(root,height=1, width=33, font='Times 31', text="EXIT", command=Close)
button1.place(x=1536, y=720, anchor='ne')


tk.Label(root, font='Times 30', text="TEXT", fg="red").pack(padx=30, pady=30)
tk.Label(root, font='Times 30 italic', text="DESCRIPTION").place(x=600, y=77)

file = "road-sign-roadtrip.gif"
info = Image.open(file)

frames = info.n_frames  # number of frames

photoimage_objects = []
for i in range(frames):
    obj = tk.PhotoImage(file=file, format=f"gif -index {i}")
    photoimage_objects.append(obj)


def animation(current_frame=0):
    global loop
    image = photoimage_objects[current_frame]

    gif_label.configure(image=image)
    current_frame = current_frame + 1

    if current_frame == frames:
        current_frame = 0

    loop = root.after(50, lambda: animation(current_frame))

gif_label = tk.Label(root, image="")
gif_label.pack()

root.mainloop()

I made only the ability to play a GIF, but only in the main window, and I need to be in the final window. In my program is quite a lot of functions (windows) and I did not write them, as it works.


Solution

  • A possible solution is to create a new project with a gif and in the main script, after closing the program, the same window with the gif appears. It is necessary to enter from maincode import * (where maincode is the name of the script with the main code) and start the program from the script with the gif in the very beginning. In this way the main script is opened and when it is closed the window with the gif appears

    from tkinter import *
    from PIL import Image, ImageTk
    from maincode import *
    
    
    
    root = Tk()
    
    # Load the GIF image
    gif = Image.open(“road-sign-roadtrip.gif”)
    
    # Create a list of frames
    frames = []
    for i in range(gif.n_frames):
        gif.seek(i)
        frames.append(ImageTk.PhotoImage(gif))
    
    # Create a label widget to display the frames
    frame_label = Label(root)
    frame_label.pack()
    
    # Define a function to play the animation
    def play_animation(frame_idx):
        root.geometry(“600x600”)
        root.title(“End program”) 
        frame_label.config(image=frames[frame_idx])
        root.after(50, play_animation, (frame_idx+1) % len(frames))
        button1 = tk.Button(root,height=1, width=15, font='Times 31', text='Exit', command=Close)
        button1.place(x=470, y=431, anchor='ne')
    
    # Start playing the animation
    play_animation(0)
    
    
    def Close(): 
        root.destroy() 
    
        
    root.mainloop()