| 1 | import gi |
| 2 | |
| 3 | gi.require_version("Adw", "1") |
| 4 | gi.require_version("Gtk", "4.0") |
| 5 | from gi.repository import Adw, Gtk |
| 6 | |
| 7 | |
| 8 | def draw_event(widget, ctx, w, h, data): |
| 9 | ctx.set_line_width(6) |
| 10 | ctx.set_tolerance(0.1) |
| 11 | |
| 12 | ctx.select_font_face("sans-serif") |
| 13 | ctx.set_font_size(48) |
| 14 | (x, y, width, height, dx, dy) = ctx.text_extents("Hello World") |
| 15 | |
| 16 | ctx.translate(100, 100) |
| 17 | |
| 18 | ctx.new_path() |
| 19 | ctx.move_to(x - 10, y - 10) |
| 20 | ctx.rel_line_to(width + 20, 0) |
| 21 | ctx.rel_line_to(0, height + 20) |
| 22 | ctx.rel_line_to(-(width + 20), 0) |
| 23 | ctx.close_path() |
| 24 | ctx.set_source_rgb(0, 0, 1) |
| 25 | ctx.stroke() |
| 26 | |
| 27 | ctx.move_to(0, 0) |
| 28 | ctx.set_source_rgb(0, 0, 0) |
| 29 | ctx.show_text("Hello World") |
| 30 | |
| 31 | |
| 32 | def on_activate(app): |
| 33 | win = Gtk.ApplicationWindow(application=app, title="Cairo GTK Text Example") |
| 34 | win.set_default_size(450, 150) |
| 35 | |
| 36 | drawingarea = Gtk.DrawingArea() |
| 37 | drawingarea.set_draw_func(draw_event, None) |
| 38 | win.set_child(drawingarea) |
| 39 | win.present() |
| 40 | |
| 41 | |
| 42 | app = Adw.Application() |
| 43 | app.connect("activate", on_activate) |
| 44 | |
| 45 | app.run(None) |