import gi gi.require_version('Gtk', '4.0') from gi.repository import Gtk, GLib, GdkPixbuf, Gdk import unicodedata def set_header_bar(window, title): header_bar = Gtk.HeaderBar() header_bar.set_show_title_buttons(True) title_widget = Gtk.Label() title_widget.set_text(title) window.set_title(title) header_bar.set_title_widget(title_widget) window.set_titlebar(header_bar) def set_margin(widget, a, b = None, c = None, d = None): if b == c == d == None: b = c = d = a elif c == d == None: (c, d) = (a, b) elif d == None: d = b widget.set_margin_top(a) widget.set_margin_end(b) widget.set_margin_bottom(c) widget.set_margin_start(d) def configure_dialog(window, parent_window, title, width=600, height=400, allow_escape=True): window.use_header_bar = True window.set_modal(True) window.set_transient_for(parent_window) window.set_default_size(width or 0, height or 0) set_header_bar(window, title) control_key = Gtk.EventControllerKey.new() if allow_escape: control_key.connect('key-pressed', on_dialog_key_pressed, window) window.add_controller(control_key) def on_dialog_key_pressed(a, b, key, c, window): if key == 9: # Escape window.close() def label(text): l = Gtk.Label() l.set_text(text) l.set_wrap(True) l.set_halign(Gtk.Align.START) return l def entry(): return Gtk.Entry() # https://gitlab.gnome.org/GNOME/pygobject/-/issues/225 # https://gist.github.com/mozbugbox/10cd35b2872628246140 def image_to_pixbuf(image): image = image.convert('RGB') data = GLib.Bytes.new(image.tobytes()) pixbuf = GdkPixbuf.Pixbuf.new_from_bytes(data, GdkPixbuf.Colorspace.RGB, False, 8, image.width, image.height, len(image.getbands())*image.width) return pixbuf.copy()