Python 如何使用Tkinter调整启动屏幕图像的大小?

Python 如何使用Tkinter调整启动屏幕图像的大小?,python,canvas,tkinter,tkinter-canvas,Python,Canvas,Tkinter,Tkinter Canvas,我正在为一个学校项目制作一个游戏,作为其中的一部分,启动屏幕将加载以模拟游戏的“加载”。我有代码,它确实会显示图像,但我希望它做的是将定义的.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' %

我正在为一个学校项目制作一个游戏,作为其中的一部分,启动屏幕将加载以模拟游戏的“加载”。我有代码,它确实会显示图像,但我希望它做的是将定义的.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图像都适合屏幕

另外,请不要对代码进行任何真正剧烈的更改,因为这符合我的要求,值更改是可以的。

我建议您使用。
代码如下:

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,可以尝试缩放(x,y)子样本(x,y)。这是你的电话号码。顺便说一句,它在我的电脑上不工作…(python2.7+win7)

我建议您使用。
代码如下:

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,可以尝试缩放(x,y)子样本(x,y)。这是你的电话号码。顺便说一句,它在我的电脑上不工作…(python2.7+win7)

问题解决了吗?如果是,请接受我的回答:)问题解决了吗?如果是,请接受我的回答:)