Как задать размеры изображения tksvg в pix?

Простой пример:

import tkinter as tk
import tksvg

window = tk.Tk()
svg_image = tksvg.SvgImage(master=window, file="128.svg", scale=1)
label = tk.Label(bg='#CCCCCC', image=svg_image, borderwidth=0, relief="solid")
print(label.winfo_reqwidth(), label.winfo_reqheight())
label.pack()
window.mainloop()

128.svg - 128х128 pix.

Вопрос: Как задать размеры изображения tksvg в pix?


Ответы (1 шт):

Автор решения: GrAnd

Размер в пикселях можно указать через параметр scaletowidth (или scaletoheight).

Из мануала Tk 8.7:

svg -dpi dpiValue -scale scaleValue -scaletowidth width -scaletoheight height

  • dpiValue is used in conversion between given coordinates and screen resolution. The value must be greater than 0 and the default value is 96.

  • scaleValue is used to scale the resulting image. The value must be greater than 0 and the default value is 1.

  • width and height are the width or height that the image will be adjusted to.

Only one parameter among -scale, -scaletowidth and -scaletoheight can be given at a time and the aspect ratio of the original image is always preserved.

→ Ссылка