Tkinter 将背景图像调整为窗口大小 [英] Tkinter resize background image to window size

查看:87
本文介绍了Tkinter 将背景图像调整为窗口大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试为我的 tkinter 窗口设置背景.我有一个方形背景图像,它的边缘逐渐变黑,然后主窗口有一个黑色背景.图像放置在背景上,如果窗口的宽度大于高度,则图像将自身居中放置在黑色背景的中间,这样看起来都非常漂亮.

Trying to set up a background for my tkinter window. I have a square background image, which fades to black around the edges, and then the main window has a black background. The image is placed over the background, and if the window is wider than it is tall, the image centers itself in the middle over the black background, and it all looks very nice.

但是当窗口的宽度和高度小于图像时,它会将图像的中心放在窗口的中心,因此您看不到整个图像,看起来有点奇怪.有没有办法调整图像大小,以便如果窗口的宽度和高度中的最大值小于图像,则将图像调整为该尺寸,保持纵横比.

However when the window is smaller than the image in width and height, it puts the center of the image in the center of the window, so you don't see the whole image, and it looks a little odd. Is there a way of resizing the image so that if the largest of the width and height of the window is smaller than the image, the image is adjusted to that size, keeping aspect ratio.

所以说背景图片是600x600:

  • 800x400 窗口中,图像不会调整大小,而是垂直居中.
  • 500x400 窗口中,图像大小调整为 500x500,并且仍然垂直居中.
  • 400x900 窗口中,图像不会调整大小,而是水平居中.
  • In a 800x400 window, the image does not resize, and centers itself vertically.
  • In a 500x400 window, the image resizes to 500x500, and still centers itself vertically.
  • In a 400x900 window, the image does not resize, and centers itself horizontally.

居中功能已经存在,我只需要调整大小功能.

The centering functionality is already there, I just need the resize functionality.

目前我拥有的是:

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 中是否有办法做到这一点?或者,如果我可能会编写自己的函数,根据窗口大小调整图像大小,但是如果用户在任何时候调整窗口大小,图像需要相对平滑和快速地调整大小.

Not sure if there is a way of doing this in tkinter? Or if perhaps I would write my own function that resizes the image according to the window size, however the image needs to resize relatively smoothly and quickly if the user resizes the window at any point.

推荐答案

这是一个示例应用程序,它使用 Pillow 在标签更改大小时调整标签上的图像大小:

This is example application that uses Pillow to resize image on the Label as the label changes size:

from tkinter import *

from PIL import Image, ImageTk

root = Tk()
root.title("Title")
root.geometry("600x600")
root.configure(background="black")



class Example(Frame):
    def __init__(self, master, *pargs):
        Frame.__init__(self, master, *pargs)



        self.image = Image.open("./resource/Background.gif")
        self.img_copy= self.image.copy()


        self.background_image = ImageTk.PhotoImage(self.image)

        self.background = Label(self, image=self.background_image)
        self.background.pack(fill=BOTH, expand=YES)
        self.background.bind('<Configure>', self._resize_image)

    def _resize_image(self,event):

        new_width = event.width
        new_height = event.height

        self.image = self.img_copy.resize((new_width, new_height))

        self.background_image = ImageTk.PhotoImage(self.image)
        self.background.configure(image =  self.background_image)



e = Example(root)
e.pack(fill=BOTH, expand=YES)


root.mainloop()

这是使用 Lenna 图像作为示例的工作原理:

This is how it works using Lenna image as example:

这篇关于Tkinter 将背景图像调整为窗口大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆