sonist-gtk/ui/image_button.py

54 lines
1.3 KiB
Python

#!/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)
css_provider = Gtk.CssProvider()
style = f"""
#ImageButton {{
border: 0;
border-radius: 50%;
background-color: transparent;
border-color:transparent;
outline: transparent;
}}
#ImageButton:hover {{
background-color: rgba(255,255,255,.1);
}}
"""
css_provider.load_from_data(style.encode('UTF-8'))
context = self.get_style_context()
context.add_provider(css_provider, Gtk.STYLE_PROVIDER_PRIORITY_USER)
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
python + gtk3开发的基于mpd后端的音乐播放器
Python 99.1%
Shell 0.9%