Johann Johann - 5 months ago 211
Python Question

Cairo and Gdk.Window with Gtk.DrawingArea using python gobject-introspection and gtk3

I am trying to follow the Gtk+ v3 tutorial found in the reference documentation. Specifically the first Drawing example using

cairo
to handle the drawing onto a
Gtk.DrawingArea
.

https://developer.gnome.org/gtk3/stable/ch01s03.html

For reference, I am using these resources:

https://python-gtk-3-tutorial.readthedocs.org/en/latest/

http://lazka.github.io/pgi-docs/

Please take a look at my (partial) translation of the
ch01s03
program into python. The main problem I have is with
configure_event_cb()
where the program should create a
cairo.Surface
object connected to a
Gdk.Window
. I do not know how to get at this
Gdk.Window
or even where to look in the reference documentation.

from gi.repository import Gtk, Gdk, cairo

surface = None

def clear_surface():
global surface
surface = cairo.Surface()
surface.set_source_rgb(1,1,1)
surface.paint()

def configure_event_cb(wid,evt):
global surface

if surface is not None:
surface.destroy()
surface = None

'''
Here, I am trying to implement the following C code:

surface = gdk_window_create_similar_surface(
gtk_widget_get_window(widget),
CAIRO_CONTENT_COLOR,
gtk_widget_get_allocated_width(widget),
gtk_widget_get_allocated_height(widget) );
'''

clear_surface()
return True

def close_window(wid):
global surface
if surface is not None:
surface.destroy()
Gtk.main_quit()

if __name__ == '__main__':
win = Gtk.Window(Gtk.WindowType.TOPLEVEL)
win.set_title('Drawing Area')
win.connect('destroy',close_window)
win.set_border_width(8)

frame = Gtk.Frame()
frame.set_shadow_type(Gtk.ShadowType.IN)
win.add(frame)

da = Gtk.DrawingArea()
da.set_size_request(100,100)
frame.add(da)

da.connect('configure-event',configure_event_cb)

win.show_all()
Gtk.main()

Answer

I have understood that the DrawingArea use the draw event now instead of the configure-event here is a simple code that works:

from gi.repository import Gtk

def draw_cb(widget, cr):
  cr.set_source_rgba(0,0,0,0.5)
  cr.rectangle(50,75,100,100)
  cr.fill()
  return False

win = Gtk.Window()

win.set_title("test")
win.set_default_size(800,600)
win.connect('delete-event', Gtk.main_quit)
da=Gtk.DrawingArea()
da.connect('draw', draw_cb)
win.add(da)
win.show_all()
Gtk.main()

But instead if you really want to create a cairo context in the callback of the expose-event, just use this to get the gtk.gdk.window:

myGdkWindow = mywin.get_window()
cr = myGdkWindow.cairo_create()