【问题标题】:Autoscale/relative scale an image in aTkinter frame自动缩放/相对缩放 aTkinter 帧中的图像
【发布时间】:2021-04-21 09:14:53
【问题描述】:

我刚刚学习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()

【问题讨论】:

  • 您可以将frame_logo 上的<Configure> 事件绑定到回调,然后根据传递给回调的事件对象的width 和height 属性调整徽标大小。
  • @acw1668 感谢您的评论!我不知道你在说什么!

标签: python python-3.x tkinter


【解决方案1】:

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

下面是一个简化的例子:

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()

【讨论】:

  • 非常感谢您的回答。它以一种奇怪的方式工作。这是我打开 GUI imgur.com/OLmaLqd 时得到的结果,这是我最大化它时发生的结果 imgur.com/GYZO79Y
  • 实际上是什么意思“它的工作方式很奇怪”?
  • 检查我链接的图像。它们变形并且不保持纵横比。
  • 是的,我的例子没有保持纵横比。而且你没有提到这个要求。
  • 我很抱歉造成混乱:/
猜你喜欢
  • 1970-01-01
  • 2012-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-11
  • 2023-03-15
  • 2012-01-14
  • 1970-01-01
相关资源
最近更新 更多