Python 3.x 使用Tkinter网格管理器将背景图像调整为窗口大小
我不知道如何使用tkinter网格管理器将背景图像调整为窗口大小。我的图像正在单独调整大小,而不调整窗口大小。它与包管理器一起工作,但我想与网格管理器一起使用Python 3.x 使用Tkinter网格管理器将背景图像调整为窗口大小,python-3.x,tkinter,Python 3.x,Tkinter,我不知道如何使用tkinter网格管理器将背景图像调整为窗口大小。我的图像正在单独调整大小,而不调整窗口大小。它与包管理器一起工作,但我想与网格管理器一起使用 from tkinter import * from PIL import Image, ImageTk root = Tk() root.title("Title") root.geometry("800x600") class Example(Frame): def __init__(self, master=None):
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
root.title("Title")
root.geometry("800x600")
class Example(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.grid(sticky=N+S+E+W)
self.image = Image.open("courbe.gif")
self.img_copy= self.image.copy()
self.background_image = ImageTk.PhotoImage(self.image)
self.background = Label(self, image=self.background_image)
self.background.grid(row =0, column =0,sticky="nsew")
self.background.grid_rowconfigure(0, weight=1)
self.background.grid_columnconfigure(0, weight=1)
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.grid(row =0, column =0,sticky="nsew")
e.grid_rowconfigure(0, weight=1)
e.grid_columnconfigure(0, weight=1)
root.mainloop()
从tkinter导入*
从PIL导入图像,ImageTk
root=Tk()
根标题(“标题”)
根几何(“800x600”)
类示例(框架):
def uuu init uuu(self,master=None):
帧。\uuuu初始化(自,主)
自网格(粘性=N+S+E+W)
self.image=image.open(“courbe.gif”)
self.img_copy=self.image.copy()
self.background\u image=ImageTk.PhotoImage(self.image)
self.background=标签(self,image=self.background\u image)
self.background.grid(行=0,列=0,sticky=“nsew”)
self.background.grid_rowconfigure(0,权重=1)
self.background.grid\u columnconfigure(0,权重=1)
self.background.bind(“”,self.\u调整大小\u图像)
def_resize_图像(自身、事件):
新建宽度=event.width
新高度=事件高度
self.image=self.img\u copy.resize((新宽度、新高度))
self.background\u image=ImageTk.PhotoImage(self.image)
self.background.configure(image=self.background\u image)
e=示例(根)
e、 网格(行=0,列=0,sticky=“nsew”)
e、 网格配置(0,权重=1)
e、 网格配置(0,权重=1)
root.mainloop()
您不应该绑定到背景更改,而应该绑定到窗口(master
)更改。然后,您可以使用master.winfo_width()
和master.winfo_height()
获取窗口的新高度和宽度
因此,在您的\uuuu init\uuuu
中使用
self.master = master
self.master.bind('<Configure>', self._resize_image)
new_width = self.master.winfo_width()
new_height = self.master.winfo_height()