3

Generally, I have been successful in using the Gtk3 system: Windows, boxes, grids, signal handlers etc. But, no matter what I try I cannot get a DrawingArea widget to work. I suspect something is wrong with my set-up but do not know how to check. Here is a bare-bones example that really should work:

#!/usr/bin/env python3 

from gi.repository import Gtk, Gdk
class MyExample(object): 
  def __init__(self, user_data=None): 
    window = Gtk.Window() 
    window.connect("destroy", Gtk.main_quit) 
    drawing_area = Gtk.DrawingArea() 
    drawing_area.set_size_request(300,300) 
    drawing_area.connect('draw',self.expose) 
    window.add(drawing_area) 
    window.show_all()  
  def expose(self,widget,cr,data): 
    print ("self_exposed") 
# === 
if __name__ == "__main__": 
    app = MyExample() 
    Gtk.main() 

When I run it I get this error:

Exception TypeError: TypeError("Couldn't find conversion for foreign struct
'cairo.Context'",) in <module 'threading' from '/usr/lib/python3.2/threading.py'>
ignored

I am using Ubuntu 12.04 and Python 3.2. I suspect something is wrong with my install but what? I have tried re-installing python-gi-cairo.

Thanks for any ideas,

Mike

Mike
  • 101
  • 1
  • 8

3 Answers3

3

Check my answer here:

Drawing in PyGobject (python3)

You need to install python3-gi-cairo package.

Community
  • 1
  • 1
Havok
  • 5,776
  • 1
  • 35
  • 44
  • Thanks Havok. It works with 2.7 as you say. I installed the python3-cairo package but that did not help it run in 3.2. If I find a solution I will post it. – Mike Jul 02 '12 at 22:35
  • There is also other minor problems when using Cairo for Python 3.x, check also this http://stackoverflow.com/a/10547095/439494 just to give you more ideas. – Havok Jul 03 '12 at 00:05
  • Gabriel Brunne give the solution, you need to install **python3-gi-cairo** to make the example code to work. – Havok Aug 23 '13 at 22:49
1

It seems the "draw" signal never gets sent when using Gtk+ 3 and Python 3. I cant figure it out but it really puts a damper on my project and is very frustrating.

Ronaldo Nascimento
  • 1,571
  • 3
  • 21
  • 40
1

I was running into similar problems, having tried the python3-cairo package to no positive results.

However, installing the python3-gi-cairo package solved it for me.

In my case, under Ubuntu 12.04, $ sudo apt-get install python3-gi-cairo in a terminal did the trick, getting me rid of the annoying "Couldn't find conversion for foreign struct 'cairo.Context'" messages.

It'll problably also work on other Ubuntu versions, maybe on Debian as well...

Other distros may have similar packages, but I can't provide much help on this.

Hope I've been of help.