简体   繁体   中英

Using GooCanvas with Python 3 and GTK+

I'm trying to write an application with a GUI, which needs a canvas. I'm using Python 3 and GTK+ 3.

I found, that GooCanvas should be appropriate for this, but couldn't even install it or get the demo to work.

Has anyone recently used this library with GTK+ 3? I couldn't find discussions on this, which surprises me. Am I missing something; is there a better canvas available, that I'm failing to find?

The version for Gtk3 is GooCanvas-2.x. There are still minor problems with GooCanvas, with the appearance of Introspection. Here's a very minimalistic example of 'modern' GooCanvas:

from gi.repository import Gtk, GooCanvas

def main(args):
    w = Gtk.Window()
    w.connect('destroy', lambda x: Gtk.main_quit())

    cv = GooCanvas.Canvas()
    cv_root = cv.get_root_item()
    rect = GooCanvas.CanvasRect(
            parent = cv_root,
            stroke_color = 'red',
            x = 10, y = 20,
            width = 40, height = 30)

    w.add(cv)
    w.show_all()
    Gtk.main()

    return 0

if __name__ == '__main__':
    import sys
    sys.exit(main(sys.argv))

The repository system takes some getting used to, but has many benefits. As far as I know, no 'better' canvases are available. You could use Cairo directly, but GooCanvas takes care of many annoying things, like redrawing the canvas automatically.

EDIT :

ImportError: cannot import name GooCanvas, introspection typelib not found

This means that your installation of introspection was not complete. I suspect you are missing the 'pgi' module in Python. ( pip3 install pgi ), and possibly an updated version of PyGObject (probably available on your distro repository)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM