#!/usr/bin/env python3 import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk, Gdk from .image import ScaleImage class ImageButton(Gtk.Button): def __init__(self, filepath, width = 26, height = 26): Gtk.Button.__init__(self) self.width = width self.height = height self._image_path = None self.set_name('ImageButton') self.set_size_request(width, height) self.set_valign(Gtk.Align.CENTER) # 针对macos的设置, 但只解决了普通状态下的边框问题, 鼠标经过的样式还在 self.set_relief(Gtk.ReliefStyle.NONE) self.set_image(filepath) def set_image(self, filepath): if self._image_path == filepath: return self._image_path = filepath image = ScaleImage(filepath) image.resize(self.width, self.height) Gtk.Button.set_image(self, image) return self