Python GTK + Canvas

I am currently learning GTK + through PyGobject and need something like a canvas. I was already looking for documents and found two widgets that seem to be able to do the job: GtkDrawingArea and GtkLayout. I need some basic functions like fillright or drawline ... Actually these functions are available from c, but I could not find how to use them with python. Can you recommend a tutorial or manpage on their python equivalents?

If you have an idea how to get something like a canvas, every tip will be appreciated. I am still involved and as long as it can be embedded in my Gtk application, I would be happy with any solution.

+4
source share
1 answer

To illustrate my comments made in the comments, let me post a quick'n'dirty PyGtk example that uses GtkDrawingArea to create a canvas and draw it with cairo

CORRECTION : you said PyGObject, i.e. Gtk + 3, so the example looks like this (the main difference is that there is no expose event, instead it draw and cairo context is already passed as a parameter):

 #!/usr/bin/python from gi.repository import Gtk import cairo import math def OnDraw(w, cr): cr.set_source_rgb(1, 1, 0) cr.arc(320,240,100, 0, 2*math.pi) cr.fill_preserve() cr.set_source_rgb(0, 0, 0) cr.stroke() cr.arc(280,210,20, 0, 2*math.pi) cr.arc(360,210,20, 0, 2*math.pi) cr.fill() cr.set_line_width(10) cr.set_line_cap(cairo.LINE_CAP_ROUND) cr.arc(320, 240, 60, math.pi/4, math.pi*3/4) cr.stroke() w = Gtk.Window() w.set_default_size(640, 480) a = Gtk.DrawingArea() w.add(a) w.connect('destroy', Gtk.main_quit) a.connect('draw', OnDraw) w.show_all() Gtk.main() 
+10
source

Source: https://habr.com/ru/post/913859/


All Articles