Python GTK+ Canvas

霸气de小男生 提交于 2019-12-06 02:55:45

问题


I'm currently learning GTK+ via PyGobject and need something like a canvas. I already searched the docs and found two widgets that seem likely to do the job: GtkDrawingArea and GtkLayout. I need a few basic functions like fillrect or drawline ... In fact these functions are available from c but I couldn't find directions how to use them from python. Can you recommend a tutorial or manpage that deals with their python equivalents ?

If you have a better idea how to get something similar to a canvas every tip would be appreciated. I'm still learning and as long as it can be embedded within my Gtk application I'd be content with any solution.


回答1:


In order to illustrate my points made in the comments, let me post a quick'n'dirty PyGtk example that uses a GtkDrawingArea to create a canvas and paints into it using cairo

CORRECTION: you said PyGObject, that is Gtk+3, so the example is as follows (the main difference is that there is no expose event, instead it is draw and a 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()


来源:https://stackoverflow.com/questions/8608686/python-gtk-canvas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!