如何在Python中设置Gtk图像的大小

Vip*_*opy 2 python gtk python-3.x gtk3

如何在Python 3中设置GTK图像宽度高度.

Vip*_*opy 7

首先 使用new_from_file_at_scale方法创建一个 GdkPixbuf,该方法具有以下语法.

new_from_file_at_scale(文件名,宽度,高度,preserve_aspect_ratio)

创建小部件Gtk.Image与方法new_from_pixbuf其接收PIXBUF作为参数.

简单示例:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GdkPixbuf

pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(
        filename="1.jpg", 
        width=24, 
        height=24, 
        preserve_aspect_ratio=True)

image = Gtk.Image.new_from_pixbuf(pixbuf)
Run Code Online (Sandbox Code Playgroud)