sonist-gtk/ui/image_button.py

38 lines
1000 B
Python
Raw Normal View History

2023-08-17 20:51:59 +08:00
#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
2023-08-18 00:09:43 +08:00
from gi.repository import Gtk, Gdk
2023-08-17 20:51:59 +08:00
from .image import ScaleImage
class ImageButton(Gtk.Button):
def __init__(self, filepath, width = 26, height = 26):
Gtk.Button.__init__(self)
self.set_name('ImageButton')
self.set_size_request(width, height)
image = ScaleImage(filepath)
image.resize(width, height)
2023-08-18 00:09:43 +08:00
# 针对macos的设置, 但只解决了普通状态下的边框问题, 鼠标经过的样式还在
self.set_relief(Gtk.ReliefStyle.NONE)
2023-08-17 20:51:59 +08:00
self.set_image(image)
css_provider = Gtk.CssProvider()
style = f"""
2023-08-18 00:09:43 +08:00
#ImageButton, #ImageButton:hover {{
2023-08-17 20:51:59 +08:00
border: 0;
background-color: transparent;
2023-08-18 00:09:43 +08:00
border-color:transparent;
outline: transparent;
2023-08-17 20:51:59 +08:00
}}
"""
css_provider.load_from_data(style.encode('UTF-8'))
context = self.get_style_context()
2023-08-18 00:09:43 +08:00
path = context.get_path()
2023-08-17 20:51:59 +08:00
context.add_provider(css_provider, Gtk.STYLE_PROVIDER_PRIORITY_USER)