python tkinter根据窗口大小调整背景图像大小

2wnc66cl  于 2023-02-11  发布在  Python
关注(0)|答案(2)|浏览(319)

在python中使用tkinter时,背景图像不会自动调整为窗口大小

以下是代码片段

self.background_image = tk.PhotoImage(file="background.png")
        self.background_label = tk.Label(self.master, image=self.background_image)
        self.background_label.pack(fill='both', expand=True)

图像应位于所有GUI组件的背景中。
代码实际上是一个python图形用户界面的一部分,但图像不会根据窗口大小自动调整大小,以前我尝试过这个,但它是添加一个静态图像,但需要图像自动适应窗口大小

这是完整的代码

import tkinter as tk
from tkinter import ttk

class MongoDBGUI:
    def __init__(self, master):
        self.master = master
        self.master.title("MongoDB Cloud Search and Display")
        self.master.geometry("1000x400")

        self.background_image = tk.PhotoImage(file="background.png")
        self.background_label = tk.Label(self.master, image=self.background_image)
        self.background_label.place(relwidth=1, relheight=1)
        
        self.label = tk.Label(self.master, text="Enter search criteria:")
        self.label.pack()
        
        self.entry = tk.Entry(self.master)
        self.entry.pack()
        
        self.search_button = tk.Button(self.master, text="Search", command=self.search)
        self.search_button.pack()
        
        self.tree = ttk.Treeview(self.master)
        self.tree.pack()
        self.tree["columns"] = ("transcript", "audio_file", "audio")
        self.tree.column("transcript", width=400, anchor="center")
        self.tree.column("audio_file", width=150, anchor="center")
        self.tree.column("audio", width=50, anchor="center")
        self.tree.heading("transcript", text="Transcript")
        self.tree.heading("audio_file", text="Audio File Number")
        self.tree.heading("audio", text="Audio")
k2arahey

k2arahey2#

这不是一个简单的任务。如果你想有一个可调整大小的图像,那么你需要使用PIL包。此外,还需要一个进一步的方法来动态地调整图像。当然,如果窗口大小总是保持不变,解决方案可以实现如下:

self.image = self.img_copy.resize((new_width, new_height))
#resize is included in the PIL.Image module

要动态调整背景图片的大小,代码可能如下所示:

import tkinter as tk
from tkinter import ttk
from scipy import misc,datasets
from PIL import Image, ImageTk


class MongoDBGUI:
    def __init__(self, master):
        self.master = master
        self.master.title("MongoDB Cloud Search and Display")
        self.master.geometry("1000x400")

        self.image = Image.open("background.png")
        self.background_image = ImageTk.PhotoImage(self.image)
        self.img_copy = self.image.copy()
        self.background_label = tk.Label(self.master, image=self.background_image)
        self.background_label.place(relwidth=1, relheight=1)

        self.label = tk.Label(self.master, text="Enter search criteria:")
        self.label.pack()

        self.entry = tk.Entry(self.master)
        self.entry.pack()

        self.search_button = tk.Button(self.master, text="Search", command=self.search)
        self.search_button.pack()

        self.tree = ttk.Treeview(self.master)
        self.tree.pack()
        self.tree["columns"] = ("transcript", "audio_file", "audio")
        self.tree.column("transcript", width=400, anchor="center")
        self.tree.column("audio_file", width=150, anchor="center")
        self.tree.column("audio", width=50, anchor="center")
        self.tree.heading("transcript", text="Transcript")
        self.tree.heading("audio_file", text="Audio File Number")
        self.tree.heading("audio", text="Audio")
        self.background_label.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_label.configure(image =  self.background_image)

master=tk.Tk()
test=MongoDBGUI(master)
master.mainloop()

相关问题