首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Tkinter将背景图像调整为窗口大小

Tkinter将背景图像调整为窗口大小
EN

Stack Overflow用户
提问于 2014-06-05 13:07:27
回答 4查看 43K关注 0票数 16

试图为我的平视窗口设置一个背景。我有一个方形的背景图像,它的边缘逐渐变黑,然后主窗口有一个黑色的背景。图像被放置在背景上,如果窗口比它高,图像中心在中间的黑色背景,这一切看起来都非常好。

然而,当窗口在宽度和高度上小于图像时,它将图像的中心放在窗口的中心,所以你看不到整个图像,它看起来有点奇怪。是否有一种调整图像大小的方法,以便如果窗口的最大宽度和高度小于图像,则将图像调整到该大小,保持高宽比。

假设背景图像是600x600

  • 800x400窗口中,图像不调整大小,并且垂直地居中。
  • 500x400窗口中,图像大小调整为500x500,并且仍然垂直地居中。
  • 400x900窗口中,图像不调整大小,并将自身水平地居中。

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

我目前拥有的是:

代码语言:javascript
运行
复制
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()

不确定是否有办法在平纹机上这样做?或者,如果我编写自己的函数,根据窗口大小调整图像大小,但是,如果用户在任何时候调整窗口大小,则图像需要相对平稳和快速地调整大小。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-06-06 05:13:18

这是一个示例应用程序,当标签更改大小时,使用枕头来调整标签上的图像大小:

代码语言:javascript
运行
复制
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()

这是使用兰娜映像作为示例的工作方式:

票数 30
EN

Stack Overflow用户

发布于 2016-03-31 12:07:14

我已经修改了上面的代码,所以它不在类中。

代码语言:javascript
运行
复制
#!/usr/bin/python3.5

from tkinter import *
from tkinter import ttk
from PIL import Image, ImageTk

root = Tk()
root.title("Title")
root.geometry('600x600')

def resize_image(event):
    new_width = event.width
    new_height = event.height
    image = copy_of_image.resize((new_width, new_height))
    photo = ImageTk.PhotoImage(image)
    label.config(image = photo)
    label.image = photo #avoid garbage collection

image = Image.open('image.gif')
copy_of_image = image.copy()
photo = ImageTk.PhotoImage(image)
label = ttk.Label(root, image = photo)
label.bind('<Configure>', resize_image)
label.pack(fill=BOTH, expand = YES)

root.mainloop()
票数 10
EN

Stack Overflow用户

发布于 2020-02-20 11:21:17

只是稍微改变了一下答案。使用self.master.winfo_width()、self.master.winfo_height()而不是'event‘使他能够更快地调整大小。

代码语言:javascript
运行
复制
import tkinter as tk
from PIL import Image, ImageTk
class Layout:
     def __init__(self,master):
       self.master = master
       self.rootgeometry()
       self.canvas = tk.Canvas(self.master)
       self.canvas.pack()
       self.background_image = Image.open('image_file.PNG') 
       self.image_copy = self.background_image.copy()
       self.background = ImageTk.PhotoImage(self.background_image)
       self.loadbackground()

    def loadbackground(self):
       self.label = tk.Label(self.canvas, image = self.background)
       self.label.bind('<Configure>',self.resizeimage)
       self.label.pack(fill='both', expand='yes')


   def rootgeometry(self):
       x=int(self.master.winfo_screenwidth()*0.7)
       y=int(self.master.winfo_screenheight()*0.7)
       z = str(x) +'x'+str(y)
       self.master.geometry(z)

  def resizeimage(self,event):
       image = self.image_copy.resize((self.master.winfo_width(),self.master.winfo_height()))
       self.image1 = ImageTk.PhotoImage(image)
       self.label.config(image = self.image1)

root = tk.Tk()
a = Styling.Layout(root)
root.mainloop()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24061099

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档