如何使 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")
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()
我只实现了播放 GIF 的功能,但只能在主窗口中,而且我需要在最终窗口中。我的程序中有很多函数(窗口),但我没有编写它们,因为它可以工作。
我觉得逻辑没问题;只是有一些拼写错误需要修复,还有一些变量需要声明。
以下是一些使 gif 变成动画的更改示例:
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)
从使用情况来看,我认为您正在为带有图像的 tkinter 应用程序开发概念验证。如果是这样的话,我希望我能帮助你更接近!如果这是针对用户的,那么它可能很难理解。 我建议考虑用户故事来开发用例。
无论如何,我建议使用具有语法突出显示功能的编辑器进行开发,例如带有 python 和 autopep8 插件的 PyCharm 或 VSCode。这些工具可以突出显示拼写错误和错误,并在将来为您节省大量时间。
编码快乐!