Bonjour,

lorsque je compile mon programme de tracé de trochoides, j'obtiens cette erreur:
File "C:\Users\Chris\Desktop\spiro2.py", line 95, in fonction
color = cb_color()
NameError: global name 'cb_color' is not defined

et je ne comprends pas du tout pourquoi cb_color n'est pas (ou mal) défini.
c'est surement une erreur assez bête mais je n'arrive pas à la trouver, si vous pouviez m'aider ce serait très sympa.
Merci d'avance

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
   1. # ==============================================================================
   2. # Tracé de Trochoïdes
   3. # ==============================================================================
   4. """Tracé de trochoïdes"""
   5. __author__  = "Gaëlle Conte"
   6. __version__ = "1.0"
   7. __date__    = "mai 2010"
   8. # ------------------------------------------------------------------------------
   9. from Tkinter import *
  10. from tkFont import Font
  11. import math
  12. import tkColorChooser
  13. #-------------------------------------------------------------------------------
  14. class debut():
  15.   """crée l'affichage du jeu"""
  16.
  17.   def __init__(self):
  18.     self.root
  19.  
  20.   global root
  21.   root = {0: Tk()}
  22.   # ----------------------------------------------------------------------------
  23.   root[1] = Frame(root[0])
  24.   root[1].pack(side=TOP, fill=BOTH)
  25.   root[11] = Frame(root[1])
  26.   root[11].pack(side=LEFT, fill=BOTH)
  27.   root[12] = Canvas(root[1], bg='white',width=500, height=500, relief=SOLID, border=3)
  28.   root[12].pack(side=LEFT, fill=BOTH, expand=YES)
  29.
  30. # ------------------------------------------------------------------------------
  31. class choix_util(): 
  32.
  33.   def __init__(self,x,y):
  34.     self.x = x
  35.     self.y = y
  36.
  37.
  38.   root[111]= Frame(root[11])
  39.   root[111].pack(side=TOP, fill=BOTH,pady=5)
  40.
  41.   root[1111]=Frame(root[111])
  42.   root[1111].pack(side=LEFT,fill=BOTH,pady=5)
  43.   root[11111]=Label(root[1111],text='position de x')
  44.   root[11111].pack(side=TOP, fill=BOTH,pady=5)
  45.   root[11112] = Scale(root[1111], orient=HORIZONTAL, to=400)
  46.   root[11112].pack(side=TOP, fill=BOTH, expand=YES)
  47.
  48.   root[1112]=Frame(root[111])
  49.   root[1112].pack(side=LEFT,fill=BOTH,pady=5)
  50.   root[11121]=Label(root[1112],text='position de y')
  51.   root[11121].pack(side=TOP, fill=BOTH,pady=5)
  52.   root[11122] = Scale(root[1112], orient=HORIZONTAL, to=400)
  53.   root[11122].pack(side=TOP, fill=BOTH, expand=YES)
  54.   # ----------------------------------------------------------------------------
  55.   root[112] = Label(root[11], text='rayon de cercle fixe (1-100)')
  56.   root[112].pack(side=TOP, fill=BOTH,pady=5)
  57.   root[113] = Scale(root[11], orient=HORIZONTAL,from_=1, to=100)
  58.   root[113].pack(side=TOP, fill=BOTH, expand=YES)
  59.   root[114] = Label(root[11], text='deplacement du rayon du cercle (1-100)')
  60.   root[114].pack(side=TOP, fill=BOTH)
  61.   root[115] = Scale(root[11], orient=HORIZONTAL, from_=1, to=100)
  62.   root[115].pack(side=TOP, fill=BOTH, expand=YES)
  63.   root[116] = Label(root[11], text='deplacement de compensation du cercle (1-100)')
  64.   root[116].pack(side=TOP, fill=BOTH)
  65.   root[117] = Scale(root[11], orient=HORIZONTAL, from_=1, to=100)
  66.   root[117].pack(side=TOP, fill=BOTH, expand=YES)
  67.
  68.   x = root[11112].get()
  69.   y = root[11122].get()
  70. # ------------------------------------------------------------------------------
  71. class spiro():
  72.   """Zone de dessin"""
  73.
  74.   global color
  75.  
  76.   # ----------------------------------------------------------------------------
  77.   def __init__(self,x,y):
  78.     self.pos_x = choix_util(x)
  79.     self.pos_y = choix_util(y)
  80.
  81.   # ------------------------------------------------------------------------------
  82.   def cb_color(self, val=None):
  83.     """permet à l'utilisateur de choisir la couleur de la courbe"""
  84.     val=tkColorChooser.askcolor()
  85.     return val
  86.  
  87.   # ----------------------------------------------------------------------------
  88.   def fonction(self, pos=None):
  89.     """ """
  90.
  91.     #a = spiro(x)
  92.     #b = spiro(y)
  93.     x = root[11112].get()
  94.     y = root[11122].get()
  95.     color = cb_color()
  96.  
  97.     R, r, O = root[113].get(), root[115].get(), root[117].get()
  98.
  99.     if pos is None:
 100.       coords=[]
 101.       for t in range(500):
 102.         coords.append((R+r)*math.cos(t)-O*math.cos(((R+r)/r)*t)+x)
 103.         coords.append((R+r)*math.sin(t)-O*math.sin(((R+r)/r)*t)+y)
 104.       outside_curve=root[11].create_line(coords,fill=color,tag='line',activedash=(2,2))
 105.      
 106.     else:
 107.       coords=[]
 108.       for t in range(500):
 109.         coords.append((R-r)*math.cos(t)+O*math.cos(((R-r)/r)*t)+self.pos_x)
 110.         coords.append((R-r)*math.sin(t)-O*math.sin(((R-r)/r)*t)+self.pos_y)
 111.       inside_curve=root[11].create_line(coords,fill=color,tag='line',activedash=(2,2))
 112.      
 113.
 114.   # ------------------------------------------------------------------------------
 115.   def cb_delete():
 116.     """supprime tous les dessins dans la fenetre"""
 117.     root[11].delete(root[11],'line')
 118.
 119.
 120.   root[118] = Frame(root[11])
 121.   root[118].pack(side=TOP,fill=BOTH,expand=YES)
 122.   root[1181] = Button(root[118], text='déplacement intérieur',command = fonction(True))
 123.   root[1181].pack(side=LEFT, fill=BOTH, expand=YES,pady=5,padx=5)
 124.   root[1182] = Button(root[118], text='déplacement extérieur',command = fonction(False))
 125.   root[1182].pack(side=LEFT, fill=BOTH, expand=YES,pady=5,padx=5)
 126.   root[119] = Button(root[11], text= 'effacer', command=cb_delete)
 127.   root[119].pack(side=TOP, fill=BOTH,padx=5,pady=2)
 128.      
 129. # ==============================================================================
 130. if __name__ == '__main__': # testcode de la classe spiro
 131.   # ----------------------------------------------------------------------------
 132.   root=debut()
 133.   root.title('Trace de Trochoides')
 134.   root.protocol('WM_DELETE_WINDOW', root.quit)
 135.   root.minsize(root.winfo_width(), root.winfo_height())
 136.   root.resizable(1,1)
 137.   root.mainloop(); root.destroy()
 138. # ==============================================================================