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()