Explications Cairo

Je voulais savoir pourquoi lorsque j'écris:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#! /usr/bin/env python
# -*- coding:Latin-1 -*-
 
import gtk
 
window = gtk.Window()
window.set_title("Animation PyGTK/Cairo")
 
dessin = gtk.DrawingArea()
dessin.set_size_request(400, 400)
ct = dessin.window.cairo_create()
ct.set_source_rgb(0.9, 0.1, 0.5)
ct.rectangle(20, 20, 50, 50)
ct.fill()
 
window.add(dessin)
window.show_all()
 
gtk.main()
Cela ne fonctionne pas, j'ai l'erreur:
"AttributeError: 'NoneType' object has no attribute 'cairo_create".

Alors que si je place la création du contexte Cairo dans une fonction, pas de souci.

Exemple:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#! /usr/bin/env python
# -*- coding:Latin-1 -*-
 
import gtk
 
 
def test(widget, data):
    ct = data.window.cairo_create()
    ct.set_source_rgb(0.9, 0.1, 0.5)
    ct.rectangle(20, 20, 50, 50)
    ct.fill()
 
 
window = gtk.Window()
window.set_title("Animation PyGTK/Cairo")
 
dessin = gtk.DrawingArea()
dessin.set_size_request(400, 400)
 
bouton1 = gtk.Button("Test")
bouton1.connect("clicked", test, dessin)
 
vbox1 = gtk.VBox(False, 3)
vbox1.pack_start(dessin)
vbox1.pack_end(bouton1)
 
window.add(vbox1)
window.show_all()
 
gtk.main()
Merci pour vos éclaircissement concernant cette énigme.

Cordialement,

Damien