试图为我的tkinter窗口设置背景。我有一个正方形的背景图像,它的边缘逐渐变黑,然后主窗口有一个黑色的背景。图像被放置在背景上,如果窗口比它的高度还宽,图像会在黑色背景的中间居中,看起来非常漂亮。
但是,当窗口的宽度和高度小于图像时,它会将图像的中心放在窗口的中心,因此您看不到整个图像,并且看起来有点奇怪。是否有调整图像大小的方法,以便如果窗口的最大宽度和高度小于图像,则将图像调整为该大小,并保持纵横比。
假设背景图像是600x600
:
800x400
窗口中,图像不调整大小,并垂直居中。500x400
窗口中,图像大小调整为500x500
,并且仍然垂直居中。400x900
窗口中,图像不会调整大小,并水平居中。定心功能已经存在,我只需要调整大小功能。
目前我拥有的是:
from tkinter import *
root = Tk()
root.title("Title")
root.geometry("600x600")
root.configure(background="black")
background_image = PhotoImage(file="Background.gif")
background = Label(root, image=background_image, bd=0)
background.pack()
root.mainloop()
不确定tkinter有没有这种方法?或者我可以编写自己的函数,根据窗口大小调整图像大小,但是如果用户在任何时候调整窗口大小,则图像需要相对平滑和快速地调整大小。
我已经修改了上面的代码,所以它不在类中
这是一个示例应用程序,当标签改变大小时,它使用枕头调整标签上的图像大小:
以下是使用Lenna图像作为示例的工作原理:
相关问题 更多 >
编程相关推荐