自动缩放/相对缩放 aTkinter 帧中的图像

时间:2021-04-15 15:04:11

标签: python python-3.x tkinter

我刚刚学习 Python,我开始使用 Tkinter 创建一个 gui。 我的主窗口由 2 个框架组成;一个在顶部,一个在底部。 在我的顶部框架上,我想放置一个图像,该图像必须占据相对于框架宽度和高度的相对位置。

我知道 label.resize((50, 50), Image.ANTIALIAS) 函数,但给它的值是绝对值。

有没有办法使用相对尺寸?

我的 MWE 如下

import tkinter as tk
from tkinter import *
from tkinter import ttk

from PIL import ImageTk, Image

import os
    
#____________________________________________________________________________________________      
#This will be the main window
window = tk.Tk()
window.geometry('500x500')
window.title("daq")

#____________________________________________________________________________________________   
#Upper frame
frame_logo = Frame(window)
frame_logo.pack(expand=1, fill="both")
frame_logo.place(rely=0.0, relx=0.0, relwidth=1.0)

# This is the logo that will be placed on the top frame
image_logo = ImageTk.PhotoImage(Image.open("images/dice.jpg").resize((50, 50), Image.ANTIALIAS))
image_logo_label = tk.Label(frame_logo, image = image_logo)
image_logo_label.pack(side = "bottom", fill = "both", expand = "yes")


#image_logo_label = image_logo_label.resize((50, 50), Image.ANTIALIAS)
#image_logo_label.place(relx=0.5, rely=0.5)
#image_logo_load   = Image.open("images/dice.jpg")
#image_logo_load = image_logo_load.resize((50, 50), Image.ANTIALIAS)
#image_logo        = ImageTk.PhotoImage(image_logo_load)
#image_label       = ttk.Label(frame_logo, image=image_logo)
#image_label.place(relx=0.8, y=1, relwidth=0.2)
#image_label.pack(fill="none", expand = 0)
#image_label.image = image_logo
#image_label.pack(expand=1, fill="both")
#image_label.place(relx=0.8, rely=0.0, relwidth=1., relheight=1.)

#____________________________________________________________________________________________   
#Lower frame
frame_main = Frame(window)
frame_main.pack(expand=1, fill="both")
frame_main.place(rely=0.10, relx=0.0, relwidth=1.0)

#Create a tabcontrol
tabControl = ttk.Notebook(frame_main)

tab_Digitizer    = ttk.Frame(tabControl)
tabControl.add(tab_Digitizer, text='   Digitizer   ')


tab_SignalViewer = ttk.Frame(tabControl)
tabControl.add(tab_SignalViewer, text='   Signal Viewer   ')

tabControl.pack(expand=1, fill="both")

#This keeps the window open - has to be at the end
window.mainloop()

1 个答案:

答案 0 :(得分:1)

您可以将 <Configure> 上的 frame_logo 事件绑定到回调,然后根据 width 对象的 heightevent 属性调整徽标图像的大小传递给回调。

下面是一个简化的例子:

import tkinter as tk
from PIL import Image, ImageTk

#__________________________________________
#This will be the main window
window = tk.Tk()
window.geometry('500x500')
window.title("daq")

#__________________________________________
#Upper frame
frame_logo = tk.Frame(window)
frame_logo.place(y=0, x=0, relwidth=1.0, relheight=0.1)

# This is the logo that will be placed on the top frame
base_image = Image.open("images/logo.png")
image_logo_label = tk.Label(frame_logo)
image_logo_label.pack(side="bottom", fill="both", expand=1)

# resize image (keeping the aspect ratio) to fit the width or height 
def resize_image(img, width, height):
    im_w, im_h = img.size
    print(im_w, im_h)
    ratio = min(width/im_w, height/im_h)
    return img.resize((int(im_w*ratio), int(im_h*ratio)), Image.ANTIALIAS)

def on_frame_logo_resize(event):
    resized = resize_image(base_image, event.width, event.height)
    image = ImageTk.PhotoImage(resized)
    image_logo_label.config(image=image)
    image_logo_label.image = image # save a reference of the image to avoid garbage collected

frame_logo.bind("<Configure>", on_frame_logo_resize)

window.mainloop()
相关问题