@tttttt6666ttttt

Как завершать программу анимированной гифкой?

Я сделал программу с помощью Python 3.12 в Tkinter, но дело в том, что когда я нажимаю "Выход" вместо окна с анимированной гифкой и кнопкой закрытия программы – ничего не происходит.

Гифку смог вставить только в главное окно, а не в специальное окно.

Как сделать окно с работающей и анимированной гифкой?

Код:
import tkinter as tk
from PIL import Image, ImageTk

def open_second_window():
    # Создаем дочернее окно
    second_window = tk.Toplevel(root)
    second_window.title("Второе окно")
    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="ОДИН", 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="ДВА", 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="ЧЕТЫРЕ", 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="ТРИ", command=open_threetenfive_window)
    button1.place(x=688, y=352, anchor='ne')


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


    # Устанавливаем режим grab_set()
    third_window.grab_set()
def open_four_window(second_window):
    # Закрываем второе окно
    second_window.destroy()

    # Создаем  дочернее окно
    four_window = tk.Toplevel(root)
    four_window.title("Третье окно")
    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="В начало", 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="Выход", command=animation)
    button1.place(x=1536, y=720, anchor='ne')

....

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="Далее", 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="Выход", command=Close)
button1.place(x=1536, y=720, anchor='ne')


tk.Label(root, font='Times 30', text="МЕНЮ", fg="red").pack(padx=30, pady=30)
tk.Label(root, font='Times 30 italic', text="ОПИСАНИЕ").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()
  • Вопрос задан
  • 19 просмотров
Пригласить эксперта
Ответы на вопрос 1
Vindicar
@Vindicar
RTFM!
У тебя в коде полно вызовов third_window.grab_set(), хотя а) никакого третьего окна ты не показал и б) даже в коде, который относится к дургим окнам. Копипаст подвел, или так задумано?
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы