Aloha cher camarades
J'ai réalisé le petit programme suivant (le but étant plus de comprendre la programmation évenementiel que de réellement connaître la température en fahrenheit).
voici le programme:
Ce programme fonctionne mais! pour corser un peu la chose, j'ai transferé la méthode " def convert " dans un autre fichier.
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 #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 5 mars 2014 @author: NZ112003 ''' from os import * from Tkinter import * from convertiontemperature import* def convert(event): calcul = float(inside.get()) calcul2 = calcul * 1.8 + 32 print calcul,'degre celsius =',calcul2,'degre fahrenheit' if __name__ == '__main__': pass window1 = Tk() can = Canvas (window1, width = 800, height = 450, bg = 'blue') # open a window can.pack( side = TOP, padx = 0, pady= 0) photo = PhotoImage(file ='C:\Users\NZ112003\workspace\Temperature\Design\Earth.gif') item = can.create_image(400, 225, image =photo) txt = Label(window1, text = 'Température (C°) :') inside = Entry(window1) inside.bind("<Return>",convert) txt.pack(side =LEFT) inside.pack(side =LEFT) "Button creation" boutton5 = Button(window1, text='close', command = window1.destroy ) boutton5.pack(side = LEFT, padx = 15, pady = 15) window1.mainloop()
Et la Paf, ca ne fait pas des Chocapic, mais cela ne fonctionne pas.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 8 avr. 2014 @author: nz112003 ''' from Tkinter import * def convert(event): calcul = float(inside.get()) calcul2 = calcul * 1.8 + 32 print calcul,'degre celsius =',calcul2,'degre fahrenheit'
Même en remplacent l'argument "event" par "inside", j'obtiens ce message d'erreur.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 Exception in Tkinter callback Traceback (most recent call last): File "C:\Program Files (x86)\python26\lib\lib-tk\Tkinter.py", line 1410, in __call__ return self.func(*args) File "C:\Users\NZ112003\workspace\Temperature\src\convertiontemperature.py", line 11, in convert calcul = float(inside.get()) NameError: global name 'inside' is not defined
Il doit y avoir un concept que je n'ai pas encore bien cerné, notamment sur la porter des variables.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 Traceback (most recent call last): File "C:\Program Files (x86)\python26\lib\lib-tk\Tkinter.py", line 1410, in __call__ return self.func(*args) File "C:\Users\NZ112003\workspace\Temperature\src\convertiontemperature.py", line 11, in convert calcul = float(inside.get()) AttributeError: Event instance has no attribute 'get'
Merci d'avance
Partager