8

I've been learning GTK3 on my own and I'm now stuck on Gtk.DrawingArea. As with most widgets and functions in GTK, there seems to be a lack of updated documentation and tutorials on this. For reference as to what I've found(hopefully this will help some other lost googlers), lazka's auto-generated documentation has been my best resource:

http://lazka.github.io/pgi-docs/Gtk-3.0/classes/index.html

However the drawing area page has no methods described.

http://lazka.github.io/pgi-docs/Gtk-3.0/classes/DrawingArea.html

This leads me to believe that I have a fundamental misunderstanding of GTK and this widget, but I can't find anything explaining how to work with it! The only tutorials I could find were these, but they are all for older versions;

3 is best of these tutorials, but I can't seem to get it to work in GTK3, and honestly its pretty advanced. Several things are also deprecated and the replacements seem to be pretty cryptic and are unclear how they work with DrawingArea: http://lazka.github.io/pgi-docs/Gtk-3.0/classes/Widget.html#Gtk.Widget.set_style

The official tutorial doesn't have a section on it, and hasn't been significantly updated in a long time:

http://python-gtk-3-tutorial.readthedocs.org/en/latest/objects.html

https://github.com/sebp/PyGObject-Tutorial

My question now is whether someone could help me understand how to draw with Gtk.DrawingArea? Is there a gtk3 tutorial out there?

My goal at the moment is to automatically draw black points on a drawing area based on an algorithm's output, and display the results. I would show my code but I barely have anything.

Swashy
  • 177
  • 3
  • 6
  • Note that `GtkDrawingArea` truly doesn't have any methods! It's just a blank widget with all the properties set so that you can draw on it in the `draw` signal. – ptomato Nov 19 '14 at 04:49
  • Interesting! I struggled with the draw signal for a while, I noticed when I connected it to a callback(on_draw), the signal would stick on and the callback was effectively in a loop! I worked around it by unblocking the handler id when I wanted it on, and then blocking it at the end of the callback, but I don't think that's the correct way to do that. Do you have further reading for making custom signals or any further advice perhaps? Also would you know a method in cairo.context for writing a single pixel? The closest I got was making very small square. – Swashy Nov 20 '14 at 16:28
  • It's best to open a new question if you have additional questions, so that everyone gets a chance to answer, but - the draw signal should get emitted every time the widget needs a redraw. (Once something is drawn, it's forgotten, and so if another window moves over top of it then you need to redraw it.) And Cairo doesn't deal in pixels, so a small square is probably correct, depending on what you're trying to achieve. – ptomato Nov 21 '14 at 04:41

1 Answers1

6

You basically connect to the ::draw signal and draw in the handler using pycairo. And use queue_draw/queue_draw_area() if you want to redraw.

See http://zetcode.com/gfx/pycairo/basicdrawing/ for examples

lazka
  • 683
  • 6
  • 7
  • 4
    Whoa lazka himself! Thanks, this is exactly what I was looking for. – Swashy Nov 17 '14 at 23:33
  • Is there any way to get the grip of the parent ImageSurface of the Cairo context too from within the draw event handler? – BarbaraKwarc Dec 20 '18 at 14:52
  • Context.get_target() should give your the target surface: https://pycairo.readthedocs.io/en/latest/reference/context.html#cairo.Context.get_target – lazka Dec 23 '18 at 19:30