RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1594989
Accepted
kot may
kot may
Asked:2024-09-27 03:17:29 +0000 UTC2024-09-27 03:17:29 +0000 UTC 2024-09-27 03:17:29 +0000 UTC

如何用 GIF 动画结束节目?

  • 772

我在 Tkinter 中使用 Python 3.12 编写了一个程序,但问题是,当我单击“退出”而不是带有动画 GIF 的窗口和用于关闭程序的按钮时,什么也没有发生。

我只能将 GIF 插入主窗口,而不能插入特殊窗口。

如何制作一个带有可运行的 gif 动画的窗口?

代码:

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()
python
  • 1 1 个回答
  • 35 Views

1 个回答

  • Voted
  1. Best Answer
    kot may
    2024-09-30T06:36:13Z2024-09-30T06:36:13Z

    一个可能的解决方案是创建一个带有 GIF 的新项目,并在主脚本中,关闭程序后,会出现带有 GIF 的相同窗口。您需要在开头输入带有 gif 的脚本 from maincode import *(其中 maincode 是带有主代码的脚本名称),然后从带有 gif 的脚本启动程序。这将打开主脚本,当您关闭它时,会出现一个带有 GIF 的窗口。

    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("Завершение программы") 
        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="Выход", command=Close)
        button1.place(x=470, y=431, anchor='ne')
    
    # Start playing the animation
    play_animation(0)
    
    
    def Close(): 
        root.destroy() 
    
        
    root.mainloop()
    
    • 0

相关问题

  • 是否可以以某种方式自定义 QTabWidget?

  • telebot.anihelper.ApiException 错误

  • Python。检查一个数字是否是 3 的幂。输出 无

  • 解析多个响应

  • 交换两个数组的元素,以便它们的新内容也反转

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5