Bonjour à tous,
Un fichier xml contient l'image d'une arborescence de répertoires (projet/slide/field/fluo/image).
Un script ouvre ce fichier, 5 combox pyqt4 devraient permettre de naviguer dans l'arbre.
Quand on choisit un item d'une des combobox les contenus des combobox filles devraient se modifier en conséquence.
Le script fonctionne pour les trois premieres combobox (projet/slide/field),
pour la quatrième je ne suis pas trop sur et la cinquième n'est pas rafraichie.

J'ai utilisé les explications suivantes pour construire le script.

Le fichier xml à l'air correct. Le probleme serait donc dans le script, mais je coince.

Merci pour vos lumières

Jean-Pat

PS
l'accès au fichier xml est en dur dans le script, à modifier pour tester.

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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 30 10:42:54 2012
 
@author: JeanPat
 
"""
import sys
import os
import lxml.etree as et
from copy import deepcopy
from PyQt4 import QtGui, QtCore
#import qimage2ndarray as qnd
#import imread
 
class CascadeMenu(QtGui.QWidget):
 
    """Four combobox to explore a tree content"""
 
    def __init__(self, etree):
        super(CascadeMenu, self).__init__()
        self.db = etree  # DataBase:The whole directory tree
        print "children's databe are projects"
        print self.db.getchildren()
 
        self.lbl = QtGui.QLabel("cytoM", self)
 
#==============================================================================
#         Connection en cascade des QCombobox
#==============================================================================
#==============================================================================
#         ## COMBO1 Projets
#==============================================================================
        #création du combo1
        self.combo1 = QtGui.QComboBox(self)#projects list
        self.label_combo1 = QtGui.QLabel("Project",self)
        # remplissage du combo1 avec les projets
        self.combo1.addItems(self.GetChildrenName(self.db))#project level-first element
        #les enfants (node2) d'un projet (node1) sont des lames (slides)        
        self.node1=self.db.getchildren()
        ##Connection combo1 vers combo2
        self.connect(self.combo1, QtCore.SIGNAL('currentIndexChanged(QString)'),self.changeindexcombo1)
        # ne faudrait-il pas connecter à combo 3 4 et 5 aussi?
#        self.connect(self.combo1, QtCore.SIGNAL('currentIndexChanged(QString)'),self.changeindexcombo2)
#        self.connect(self.combo1, QtCore.SIGNAL('currentIndexChanged(QString)'),self.changeindexcombo3)
#        self.connect(self.combo1, QtCore.SIGNAL('currentIndexChanged(QString)'),self.changeindexcombo4)
#        self.connect(self.combo1, QtCore.SIGNAL('currentIndexChanged(QString)'),self.changeindexcombo5)
 
#==============================================================================
#                                   COMBO2 Slides
#==============================================================================
 
        #création combo2
        self.combo2 = QtGui.QComboBox(self)#slides list
        self.label_combo2 = QtGui.QLabel("Slide",self)
        #         Fill combo2 with slides belonging to a project 
        self.node2=self.node1[0].getchildren()
        #fill from combo1 active item as parent
        parent=self.node1[self.combo1.currentIndex()]
        childrenlist=self.GetChildrenName(parent)
        #print "init combo2 :childrenlist",childrenlist
        self.combo2.addItems(QtCore.QStringList(childrenlist))
        ##Connection combo2 vers combo3
        self.connect(self.combo2, QtCore.SIGNAL('currentIndexChanged(QString)'),self.changeindexcombo2)
#        self.connect(self.combo2, QtCore.SIGNAL('currentIndexChanged(QString)'),self.changeindexcombo3)
#        self.connect(self.combo2, QtCore.SIGNAL('currentIndexChanged(QString)'),self.changeindexcombo4)
#        self.connect(self.combo2, QtCore.SIGNAL('currentIndexChanged(QString)'),self.changeindexcombo5)
 
#==============================================================================
#       COMBO3: field (location xy on the slide)
#==============================================================================
 
 
        self.combo3 = QtGui.QComboBox(self)#fields list
        self.label_combo3 = QtGui.QLabel("Field",self)
        #fill from combo1 active item as parent
        self.node3=self.node2[0].getchildren()# 15 mai2012: a quoi sert self.node3?
        parent=self.node2[self.combo2.currentIndex()]
        childrenlist=self.GetChildrenName(parent)
        #print "init combo3 :childrenlist",childrenlist
        self.combo3.addItems(QtCore.QStringList(childrenlist))        
 
        ##Connection combo3 vers combo4 
        self.connect(self.combo3, QtCore.SIGNAL('currentIndexChanged(QString)'),self.changeindexcombo3)
#        self.connect(self.combo3, QtCore.SIGNAL('currentIndexChanged(QString)'),self.changeindexcombo4)
#        self.connect(self.combo3, QtCore.SIGNAL('currentIndexChanged(QString)'),self.changeindexcombo5)
 
 
#==============================================================================
#       COMBO4: fluorochrome
#==============================================================================
        # creation combo4                                   
        self.combo4 = QtGui.QComboBox(self)
        self.label_combo4 = QtGui.QLabel("Fluo",self)
        #remplissage avec les enfants du parent actif de combo3
        self.node4=self.node3[0].getchildren()
        print "init node4",self.node4
        parent=self.node3[self.combo3.currentIndex()]
        childrenlist=self.GetChildrenName(parent)
        self.combo4.addItems(QtCore.QStringList(childrenlist))
        #Connection combo4 vers combo5; appel de chargement d'un fluo
        self.connect(self.combo4, QtCore.SIGNAL('currentIndexChanged(QString)'),self.changeindexcombo4)
#        self.connect(self.combo4, QtCore.SIGNAL('currentIndexChanged(QString)'),self.changeindexcombo5)
#==============================================================================
#       COMBO5: image                                    
#==============================================================================
 
        ##Connection combo5 vers : appel d'une image                                     
        self.combo5 = QtGui.QComboBox(self)#Fluorochromes list
        self.label_combo5 = QtGui.QLabel("Image",self)
        #remplissage
        self.node5=None                #fluo level
        self.node5=self.node4[0].getchildren()
        print "init node5",self.node5
        parent=self.node4[self.combo4.currentIndex()]
        childrenlist=self.GetChildrenName(parent)
        self.combo5.addItems(QtCore.QStringList(childrenlist))
        #connection, combo5 et combo5: euh?
        self.connect(self.combo5, QtCore.SIGNAL('currentIndexChanged(QString)'),self.changeindexcombo5)
 
#        ndimage=imread.imread("/home/claire/Applications/ImagesTest/MFISH/Dapi.tif")
#        convert=qnd.array2qimage(ndimage,normalize=True)
#        qpix = QtGui.QPixmap(convert)
        image = QtGui.QLabel(self)
        #image.setPixmap(qpix)
 
        # positionnement des widgets dans la fenêtre
        posit = QtGui.QGridLayout()
        posit.addWidget(self.label_combo1, 0, 0)
        posit.addWidget(self.label_combo2, 0, 1)
        posit.addWidget(self.label_combo3, 0, 2)
        posit.addWidget(self.label_combo4, 0, 3)
        posit.addWidget(self.label_combo5, 0, 4)
        posit.addWidget(self.combo1, 1, 0)
        posit.addWidget(self.combo2, 1, 1)
        posit.addWidget(self.combo3, 1, 2)
        posit.addWidget(self.combo4, 1, 3)
        posit.addWidget(self.combo5, 1, 4)
        posit.addWidget(self.lbl, 2, 0)
 
        posit.addWidget(image, 2, 0)
        self.setLayout(posit)
        self.setWindowTitle('Cascade Menus')
        self.show()
 
        pathto=os.path.join(str(self.combo1.currentText()),\
                            str(self.combo2.currentText()),\
                            str(self.combo3.currentText()),\
                            str(self.combo4.currentText()),\
                            str(self.combo5.currentText()))
        print "pathto",pathto
        self.lbl.setText(str(pathto))
 
 
    def GetChildrenName(self,etree):
        """return the names (str) of the children of a Database etree.Element"""
        childrenlist=[]
        #print "GetChildren ... type(etree)",type(etree[0])
        children=etree.getchildren()
        #print children[0].get("name")
        for c in children:
            childrenlist.append(c.get("name"))
        return childrenlist
 
    def changeindexcombo1(self):
        """méthode exécutée en cas de changement d'affichage du combo1
        """
        #vide le contenu du combo enfant
        self.combo2.clear()
        #quel le parent?
        #recup l'item actif de combo1
        print "combo1 cur Index",self.combo1.currentIndex()
        #identifier le node correspondant,
        ## suppossons que ce soit==self.combo1.currentIndex()
        ##parent est un etree.Element, son nom devrait être l'item courant de combo1
        parent=self.node1[self.combo1.currentIndex()]
        #demander ses enfants,
        ##demander le nom des enfants
        childrenlist=self.GetChildrenName(parent)
        self.combo2.addItems(QtCore.QStringList(childrenlist))
        pathto=os.path.join(str(self.combo1.currentText()),\
                            str(self.combo2.currentText()),\
                            str(self.combo3.currentText()),\
                            str(self.combo4.currentText()),\
                            str(self.combo5.currentText()))
        self.lbl.setText(str(pathto))
 
    def changeindexcombo2(self):
        """méthode exécutée en cas de changement d'affichage du combo2
        """
        self.combo3.clear()
        #quel le parent?
        #recup l'item actif de combo1
        print "combo2 cur Index",self.combo2.currentIndex()
        #identifier le node correspondant,
        ## suppossons que ce soit==self.combo1.currentIndex()
        ##parent est un etree.Element, son nom devrait être l'item courant de combo1
        parent=self.node2[self.combo2.currentIndex()]
        #demander ses enfants,
        ##demander le nom des enfants
        childrenlist=self.GetChildrenName(parent)
        self.combo3.addItems(QtCore.QStringList(childrenlist))
        pathto=os.path.join(str(self.combo1.currentText()),\
                            str(self.combo2.currentText()),\
                            str(self.combo3.currentText()),\
                            str(self.combo4.currentText()),\
                            str(self.combo5.currentText()))
        self.lbl.setText(str(pathto))
 
    def changeindexcombo3(self):
        """méthode exécutée en cas de changement d'affichage du combo3
        """
        self.combo4.clear()
        #quel le parent?
        #recup l'item actif de combo1
        print "combo3 cur Index",self.combo3.currentIndex()
 
        parent=self.node3[self.combo3.currentIndex()]
        #demander ses enfants,
        ##demander le nom des enfants
        childrenlist=self.GetChildrenName(parent)
        print "parent",parent
        print "combo3",childrenlist
        self.combo4.addItems(QtCore.QStringList(childrenlist))
        pathto=os.path.join(str(self.combo1.currentText()),\
                            str(self.combo2.currentText()),\
                            str(self.combo3.currentText()),\
                            str(self.combo4.currentText()),\
                            str(self.combo5.currentText()))
        self.lbl.setText(str(pathto))
 
    def changeindexcombo4(self):
        """méthode exécutée en cas de changement d'affichage du combo4
        """
        self.combo5.clear()
        print "combo4 cur Index",self.combo4.currentIndex()
        parent=self.node4[self.combo4.currentIndex()]
        #demander ses enfants,
        ##demander le nom des enfants
        childrenlist=self.GetChildrenName(parent)
        self.combo5.addItems(QtCore.QStringList(childrenlist))
        pathto=os.path.join(str(self.combo1.currentText()),\
                            str(self.combo2.currentText()),\
                            str(self.combo3.currentText()),\
                            str(self.combo4.currentText()),\
                            str(self.combo5.currentText()))
        self.lbl.setText(str(pathto))
 
    def changeindexcombo5(self):
        """méthode exécutée en cas de changement d'affichage du combo5
        """
        #self.combo5.clear()#surtout pas!!
        #quel le parent?
        #recup l'item actif de combo4
        print "combo5 cur Index",self.combo5.currentIndex()
        #identifier le node correspondant4
        ## suppossons que ce soit==self.combo1.currentIndex()
        ##parent est un etree.Element, son nom devrait être l'item courant de combo1
        #parent=self.node5[self.combo5.currentIndex()]
        #demander ses enfants,
        ##demander le nom des enfants
#        childrenlist=self.GetChildrenName(parent)
#        print "from changeindex combo5",childrenlist
#        self.combo5.addItems(QtCore.QStringList(childrenlist))
        pathto=os.path.join(str(self.combo1.currentText()),\
                            str(self.combo2.currentText()),\
                            str(self.combo3.currentText()),\
                            str(self.combo4.currentText()),\
                            str(self.combo5.currentText()))
        self.lbl.setText(str(pathto))
 
def main():
#==============================================================================
#    Load a xml tree
#==============================================================================
#   bug 
    root=et.parse('/home/simon/DatabaseMFISH.xml').getroot()
    p1=list(root)[0] #try to remove a project to see what's happen
    root.remove(p1)
    print "root type",type(root)
    print root.getchildren()
    #print(et.tostring(root,pretty_print=True))
    app = QtGui.QApplication(sys.argv)
    ex = CascadeMenu(root)
    sys.exit(app.exec_())
 
if __name__ == '__main__':
    main()