Bonjour tout le monde,
Je débute dans la programmation avec Python (et dans la programmation tout court).
J'essaye actuellement de programmer une interface graphique en utilisant Tkinter et en me basant sur l'exemple simpleapp_tk.py de Sébastien Sauvage ( http://sebsauvage.net/python/gui/index_fr.html ).
J'ai adapté le code original à mon projet et... cela ne fonctionne pas.
Voici le principe :
1°) déclaration de la classe guiCustom
2°) définition d'une fonction execCustom, qui instancie la classe précédente et l'exécute.Code:
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 class guiCustom(Tkinter.Tk): def __init__(self, parent): Tkinter.Tk.__init__(self,parent) self.parent = parent self.initialize() def initialize(self): self.grid() self.entryVariable = Tkinter.StringVar() self.entry = Tkinter.Entry(self,textvariable=self.entryVariable) self.entry.grid(column=0,row=0,sticky='EW') self.entry.bind("<Return>", self.OnPressEnter) self.entryVariable.set(u"Enter text here.") button = Tkinter.Button(self,text=u"Click me !", command=self.OnButtonClick) button.grid(column=1,row=0) self.grid_columnconfigure(0,weight=1) self.resizable(True,False) self.update() self.geometry(self.geometry()) self.entry.focus_set() self.entry.selection_range(0, Tkinter.END) self.protocol("WM_DELETE_WINDOW",quit) def OnButtonClick(self): getStat = self.entryVariable.get() print 'getStat vaut: ' + getStat getStat = NistCustom.makeString2202(getStat) print 'getStat vaut: ' + getStat NistCustom.fileToList(getStat) self.entry.focus_set() self.entry.selection_range(0, Tkinter.END) def OnPressEnter(self,event): getStat = self.entryVariable.get() print 'getStat vaut: ' + getStat getStat = NistCustom.makeString2202(getStat) print 'getStat vaut: ' + getStat NistCustom.fileToList(getStat) self.entry.focus_set() self.entry.selection_range(0, Tkinter.END)
3°) dans le programme principal, une fenêtre (root0), possède un bouton (butOpenChoice) qui appelle la fonction précédente.Code:
1
2
3
4
5 def execCustomNIST(): app = guiCustomNIST(None) app.title('NIST Customizator') app.mainloop()
Lorsque j'exécute le code, la fenêtre instanciée par execCustom() s'ouvre bien mais le champ de texte entryVariable apparait vide alors qu'il devrait contenir "Enter text here." ...Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 def main(): root0 = Tkinter.Tk() root0.title('Choix de statuts') root0.iconbitmap('NistGenerator.ico') startMsg = 'Blablabla...\n' """Création d'un champ d'entrée avec 3 boutons radio""" tex = Tkinter.Label(root0, text=startMsg, fg='black') tex.pack() # Liste des boutons radio radBut=[('truc', 't'), ('machin', 'm'), ('bidule', 'b')] # Le choix actuel est mémorisé dans un 'objet-variable' Tkinter : choixStatut = Tkinter.StringVar() # Par défaut, truc est sélectionné choixStatut.set('t') # Création des trois boutons radio : for text, value in radBut: Tkinter.Radiobutton(root0, text=text, value=value, variable=choixStatut, command=choixStatut.get).pack(anchor=Tkinter.CENTER) butVal = Tkinter.Button(root0, text='Valider', command = root0.destroy) butOpenChoice = Tkinter.Button(root0, text='Appeler execCustom',command=execCustom) butOpenChoice.pack() butVal.pack() root0.protocol("WM_DELETE_WINDOW",quit) root0.mainloop()
Je ne comprends pas ce qu'il se passe. :?
Je pense qu'il s'agit d'une histoire d'héritage (parent, self & Co.) mais un détail doit sûrement m'échapper... :roll:
S'il-vous-plaît, quelqu'un pourrait-il m'éclairer sur ce point obscur ?
N'hésitez pas à me poser des questions si quelque chose ne vous semble pas clair dans ma requête.
En vous remerciant d'avance :)