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.
5
I think the logic is fine; there are just some typos that need fixing and some variables that need declaring.
Here are some examples of changes that made the gif animate for me:
def open_second_window():
# omitting declarations…
button1 = tk.Button(
second_window, # omitting props…
command=open_third_window) # Changed from undefined "open_five_window"
button = tk.Button(
second_window,
command=open_third_window)# Typo: was "open_threee_window"
# Renaming def open_theend_window():
def open_third_window():
# Updated from tkinter. to tk. since that's how you imported it
third_window = tk.Toplevel(root)
photo = tk.PhotoImage(file='road-sign-roadtrip.gif')
button = tk.Button(
third_window,
command=open_second_window)
Judging from the usage I think you’re developing a proof of concept for a tkinter app with images. If that’s the case, I hope I helped you get closer! If this is intended for users, then it might be too challenging to understand.
I would recommend considering user stories for developing the use cases.
Regardless, I recommend developing with an editor that has syntax highlighting, like PyCharm or VSCode with python and autopep8 plugins. These tools can highlight typos and mistakes and save you a lot of time in the future.
Happy coding!
1