如何使用 Tkinter 调整启动画面图像的大小?

How to resize a Splash Screen Image using Tkinter?

我正在为一个学校项目开发游戏,作为其中的一部分,将加载启动画面以模拟游戏的 'loading'。我有代码,它确实显示了图像,但我想要它做的是将定义的 .gif 文件显示到屏幕上。这是代码:

import tkinter as tk
root = tk.Tk()
root.overrideredirect(True)
width = root.winfo_screenwidth()
height = root.winfo_screenheight()
root.geometry('%dx%d+%d+%d' % (width*1, height*1, width*0, height*0))
image_file = "example.gif"
image = tk.PhotoImage(file=image_file)
canvas = tk.Canvas(root, height=height*1, width=width*1, bg="darkgrey")
canvas.create_image(width*1/2, height*1/2, image=image)
canvas.pack()
root.after(5000, root.destroy)
root.mainloop()

我唯一的问题是,由于图像比屏幕大,所以不能作为一个整体显示。我如何使用此代码调整图像大小,使任何 .gif 图像都能显示在屏幕上?

P.S。请不要对代码进行任何真正的重大更改,因为这正是我想要的,值更改是可以的。

我推荐你使用PIL
这是代码:

from PIL import Image, ImageTk
import Tkinter as tk

root = tk.Tk()
root.overrideredirect(True)
width = root.winfo_screenwidth()
height = root.winfo_screenheight()
root.geometry('%dx%d+%d+%d' % (width*1, height*1, width*0, height*0))
image = Image.open(image_path)
image = image.resize((width, height), Image.ANTIALIAS)
image = ImageTk.PhotoImage(image)
canvas = tk.Canvas(root, height=height*1, width=width*1, bg="darkgrey")
canvas.create_image(width*1/2, height*1/2, image=image)
canvas.pack()
root.after(5000, root.destroy)
root.mainloop()

您从以下位置获取宽度和高度:

width = root.winfo_screenwidth()
height = root.winfo_screenheight()

然后调用 resize()

如果您仍然想使用 PhotoImage,可以尝试 zoom(x, y)subsample(x, y)。这里是doc。顺便说一句,它在我的电脑上不起作用...(python2.7 + win7)