4

The simplest example. We create a window by means of Gtk, we add there area for drawing of Gtk.DrawingArea and on it we draw the text by means of Cairo.

Example:

#!/usr/bin/env python
from gi.repository import Gtk
import cairo

class MyWindow (Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title='MyWindow')

        darea = Gtk.DrawingArea()
        darea.connect('draw', self.on_draw)
        self.add(darea)

    def on_draw(self, widget, ctx):
        ctx.set_source_rgb(0, 0, 0)
        ctx.select_font_face("Sans", cairo.FONT_SLANT_NORMAL,
            cairo.FONT_WEIGHT_NORMAL)
        ctx.set_font_size(20)
        ctx.move_to(10, 20)
        ctx.show_text("Text...")

win = MyWindow()
win.connect('delete-event', Gtk.main_quit)
win.show_all()
Gtk.main()

At me everything perfectly works at Python 2.7, but only it is necessary to change Python for Python3 and the text isn't drawn any more. In what there can be a problem?

2
  • Works for me with Python 3.2.3 on Ubuntu 12.10. Which distribution are you using? Commented Nov 14, 2012 at 8:29
  • I use Ubuntu 12.04. On how many I know in ubuntu 12.10 python 3 is used by default? And in Ubuntu 12.04 python 2.7 is used. Can to me any established packages doesn't suffice? Commented Nov 15, 2012 at 5:11

1 Answer 1

3

Had the same problem... turns out you'll get no complaints if you're missing a few packages.

try apt-get install python3-gi-cairo and give it another try. Worked for me (including your code).

Sign up to request clarification or add additional context in comments.

1 Comment

Is there an python3-gi-cairo equivalent for RPM-base repository?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.