import sys import traceback from typing import Dict, List, Optional, Sequence, Tuple, TypeAlias import gi gi.require_version("Gtk", "3.0") from gi.repository import GLib, Gio, Gtk, GObject gi.require_foreign("cairo") import cairo class CustomDrawingArea(Gtk.DrawingArea): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.set_size_request(200, 200) self.set_app_paintable(True) self.connect_after("draw", self.on_draw, {}) def on_draw(self, widget: Gtk.DrawingArea, cr: cairo.Context, data: GObject.GPointer): width = widget.get_allocated_width() height = widget.get_allocated_height() print(f"w: {width}, h: {height}") sctx = widget.get_style_context() Gtk.render_background(sctx, cr, 0, 0, width, height) cr.set_source_rgba(1.0, 0.0, 0.0, 1.0) cr.rectangle(0, 0, width, height) cr.fill() class MainWindow(Gtk.ApplicationWindow): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.app: Application = self.get_application() # type: ignore assert self.app is not None box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) self.add(box) self.area = CustomDrawingArea() frame = Gtk.Frame(label="DrawingArea") frame.add(self.area) box.pack_start(frame, expand=True, fill=True, padding=20) box.add(Gtk.Button(label=":)")) class Application(Gtk.Application): def __init__(self, *args, **kwargs): super().__init__( *args, application_id="zone.lunareclipse.draw_test", flags=Gio.ApplicationFlags.FLAGS_NONE, **kwargs ) self.window = None def do_activate(self): self.window = self.window or MainWindow(application=self) self.window.show_all() if __name__ == "__main__": app = Application() app.run(sys.argv)