Cairo example works with Python 2.7 but does not work in Python 3

The simplest example. We create a window using Gtk, add a drawing area to Gtk.DrawingArea, and on it we draw text using 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() 

Everything works fine for me on Python 2.7, but I just need to change Python for Python3, and the text is no longer drawn. What could be the problem?

+4
source share
1 answer

If the same problem ... it turns out you won’t get any complaints if you are missing a few packages.

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

+3
source

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


All Articles