Salut a tous,

J'ai crée un programme qui fonctionne bien, et qui répond à mon cahier de charge.
Mais j'aimerai que vous m'aidez a programmer autrement, en structurant mon code. Surtout tout les "if" que je met, à la fin du projet mon programme aura 10 000 ligne de code .
Montrer moi un exemple, et je construit la suite.

Voici le code :

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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
 
#!/usr/bin/env python
# -*- coding: cp1252 -*-
#SPEC 0.4
 
import sys
import time
import serial
import binascii
 
from PyQt4 import QtCore, QtGui
 
class MyPopup(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
 
        ''' Titre_1  '''
        self.Label_1 = QtGui.QLabel()
        self.Label_1.setText("NUMERO DE PORT")
 
        ''' Titre_11  '''
        self.Label_11 = QtGui.QLabel()
        self.Label_11.setText("VITESSE")
 
        ''' ComboBox_11 '''
        self.ComboBox_11 = QtGui.QComboBox()
        self.ComboBox_11.setGeometry(QtCore.QRect(10,10,10,10))
        self.ComboBox_11.addItem("COM 1")
        self.ComboBox_11.addItem("COM 25")
        self.ComboBox_11.addItem("COM 2")
        self.ComboBox_11.addItem("COM 4")
 
        ''' ComboBox_22 '''
        self.ComboBox_22 = QtGui.QComboBox()
        self.ComboBox_22.setGeometry(QtCore.QRect(10,10,10,10))
        self.ComboBox_22.addItem("4800")
        self.ComboBox_22.addItem("115200")
 
        self.bouton1 = QtGui.QPushButton("Open", self)
        self.bouton1.clicked.connect(self.F_Open)
 
        self.bouton2 = QtGui.QPushButton("Close", self)
        self.bouton2.clicked.connect(self.F_Close)
 
        # positionner les widgets dans la fenêtre
        posit = QtGui.QGridLayout()
        posit.addWidget(self.Label_1,            0, 0)
        posit.addWidget(self.ComboBox_11,        1, 0)
        posit.addWidget(self.Label_11,           2, 0)
        posit.addWidget(self.ComboBox_22,        3, 0)
        posit.addWidget(self.bouton1,            4, 0)
        posit.addWidget(self.bouton2,            5, 0)
        self.setLayout(posit)
 
 
        # SERIAL #
 
    def F_Open(self):
        F_Serial(1,self.ComboBox_11.itemText(self.ComboBox_11.currentIndex()),self.ComboBox_22.itemText(self.ComboBox_22.currentIndex()))
 
 
    def F_Close(self):
        F_Serial(4,"null", "null")
 
 
    def M_Signal(self):
        self.M_Status(3)
        time.sleep(0.2)
        self.M_Status(2)
 
 
def F_Serial(a,port_ou_msg,vitesse):
    s = "null"   
    if a == 1:
         num_port = int(port_ou_msg[4:6])
         vit = int(vitesse)
         global ser
         ser = serial.Serial(num_port-1,vit,timeout=0,parity='N', rtscts=1)
         print ser.portstr # check which port was really used
    elif a == 2:
         ser.write(port_ou_msg+"\r\n") # Ecriture verticale
    elif a == 3:
         s = ser.readline(120)
    elif a == 4:
         ser.close()
    return s
 
class essai(QtGui.QMainWindow):
    def __init__(self):
#--------------------------------------------
        # Main Window #
        QtGui.QMainWindow.__init__(self)
        self.resize(310,310)
        self.setWindowTitle(' Travaille ')
 
        # Menu #
        ''' Menu0 '''
        Action1 = QtGui.QAction('LOAD SGV', self)   
        Action1.setStatusTip('Action 1')  
        Action1.triggered.connect(self.M_Action1)
 
        Action2 = QtGui.QAction('SAVE SGV', self)   
        Action2.setStatusTip('Action 2')  
        Action2.triggered.connect(self.M_Action2)
 
        Action3 = QtGui.QAction('EXPORT TO MOBILE', self)   
        Action3.setStatusTip('Action 3')
        Action3.triggered.connect(self.M_Action3)
 
        menubar = self.menuBar()
 
        ChMenu = menubar.addMenu('&Action')
        ChMenu.addAction(Action1)
        ChMenu.addAction(Action2)
        ChMenu.addAction(Action3)
 
        '''Menu2'''
        PopFen1 = QtGui.QAction('PopUp', self)
        PopFen1.setStatusTip('PopFen1')  
        PopFen1.triggered.connect(self.F_Popup)
 
        menubar = self.menuBar()
 
        PopMenu = menubar.addMenu('&PopFen')
        PopMenu.addAction(PopFen1)
#--------------------------------------------
        ''' Dock '''
 
        self.Label_0 = QtGui.QLabel()
        self.Label_0.setText("MODE")
 
        self.ComboBox_0 = QtGui.QComboBox()
        self.ComboBox_0.setGeometry(QtCore.QRect(10,10,10,10))
        self.ComboBox_0.addItem("HANDSET")
        self.ComboBox_0.addItem("HANDFREE")
        self.ComboBox_0.addItem("CAR KIT")
        self.ComboBox_0.addItem("RSM")
 
        self.Label_1 = QtGui.QLabel()
        self.Label_1.setText("SIGNAL")
 
        self.ComboBox_1 = QtGui.QComboBox()
        self.ComboBox_1.setGeometry(QtCore.QRect(10,10,10,10))
        self.ComboBox_1.addItem("VOICE")
        self.ComboBox_1.addItem("RING")
        self.ComboBox_1.addItem("BEEP")
 
 
        VBoxLayout_1 = QtGui.QVBoxLayout()
        VBoxLayout_1.addWidget(self.Label_0)
        VBoxLayout_1.addWidget(self.ComboBox_0)
        VBoxLayout_1.addWidget(self.Label_1)
        VBoxLayout_1.addWidget(self.ComboBox_1)
        VBoxLayout_1.setSpacing(100)
 
        Widget_1 = QtGui.QWidget()
        Widget_1.setLayout(VBoxLayout_1)
 
        F_Dock_Wid_Vitesse = QtGui.QDockWidget()
        F_Dock_Wid_Vitesse.setWidget(Widget_1)
        self.addDockWidget(QtCore.Qt.LeftDockWidgetArea, F_Dock_Wid_Vitesse)
 
        self.SpinBox_1 = QtGui.QSpinBox(self)
        self.SpinBox_1.setRange(-12,12)
        self.SpinBox_1.setSingleStep(1)
        self.SpinBox_1.setValue(0)
 
        self.SpinBox_2 = QtGui.QSpinBox(self)
        self.SpinBox_2.setRange(-12,12)
        self.SpinBox_2.setSingleStep(1)
        self.SpinBox_2.setValue(0)
 
        self.SpinBox_3 = QtGui.QSpinBox(self)
        self.SpinBox_3.setRange(-12,12)
        self.SpinBox_3.setSingleStep(1)
        self.SpinBox_3.setValue(0)
 
        GridLayout_1 = QtGui.QGridLayout()
        GridLayout_1.addWidget(self.SpinBox_1,     0,0,1,1)
        GridLayout_1.addWidget(self.SpinBox_2,     0,1,1,1)
        GridLayout_1.addWidget(self.SpinBox_3,     0,2,1,1)
 
        O_GroupBox_1 = QtGui.QGroupBox()
        O_GroupBox_1.setLayout(GridLayout_1)
 
        self.setCentralWidget(O_GroupBox_1)
#--------------------------------------------
        self.ComboBox_0.currentIndexChanged.connect(self.test_1)
        self.ComboBox_1.currentIndexChanged.connect(self.test_1)
 
    def test_1(self, idx):
        a = [self.SpinBox_1.value()]
        b = [self.SpinBox_2.value()]
        c = [self.SpinBox_3.value()]
        Tab_0 = [a, b, c]
        if str(self.ComboBox_0.currentText()) == "HANDSET" :
            if str(self.ComboBox_1.currentText()) == "VOICE" :
               Tab = [a, b, c]
               print "Voici le résultat de Tab_0 :"
               print Tab_0
 
 
        a_1 = [self.SpinBox_1.value()]
        b_1 = [self.SpinBox_2.value()]
        c_1 = [self.SpinBox_3.value()]
        Tab_1 = [a_1, b_1, c_1]
        if str(self.ComboBox_0.currentText()) == "HANDFREE" :
            if str(self.ComboBox_1.currentText()) == "VOICE" :
               Tab_1 = [a_1, b_1, c_1]
               print "Voici le résultat de Tab_1 :"
               print Tab_1
 
 
        a_2 = [self.SpinBox_1.value()]
        b_2 = [self.SpinBox_2.value()]
        c_2 = [self.SpinBox_3.value()]
        Tab_2 = [a_2, b_2, c_2]
        if str(self.ComboBox_0.currentText()) == "CAR KIT" :
            if str(self.ComboBox_1.currentText()) == "VOICE" :
               Tab_2 = [a_2, b_2, c_2]
               print "Voici le résultat de Tab_2 :"
               print Tab_2
 
        a_3 = [self.SpinBox_1.value()]
        b_3 = [self.SpinBox_2.value()]
        c_3 = [self.SpinBox_3.value()]
        Tab_3 = [a_3, b_3, c_3]
        if str(self.ComboBox_0.currentText()) == "RSM" :
            if str(self.ComboBox_1.currentText()) == "VOICE" :
               Tab_3 = [a_3, b_3, c_3]
               print "Voici le résultat de Tab_3 :"
               print Tab_3
 
        a_4 = [self.SpinBox_1.value()]
        b_4 = [self.SpinBox_2.value()]
        c_4 = [self.SpinBox_3.value()]
        Tab_4 = [a_4, b_4, c_4]
        if str(self.ComboBox_0.currentText()) == "HANDSET" :
            if str(self.ComboBox_1.currentText()) == "RING" :
               Tab_4 = [a_4, b_4, c_4]
               print "Voici le résultat de Tab_4 :"
               print Tab_4
 
        a_5 = [self.SpinBox_1.value()]
        b_5 = [self.SpinBox_2.value()]
        c_5 = [self.SpinBox_3.value()]
        Tab_5 = [a_5, b_5, c_5]
        if str(self.ComboBox_0.currentText()) == "HANDFREE" :
            if str(self.ComboBox_1.currentText()) == "RING" :
               Tab_5 = [a_5, b_5, c_5]
               print "Voici le résultat de Tab_5 :" 
               print Tab_5
 
 
        a_6 = [self.SpinBox_1.value()]
        b_6 = [self.SpinBox_2.value()]
        c_6 = [self.SpinBox_3.value()]
        Tab_6 = [a_6, b_6, c_6]
        if str(self.ComboBox_0.currentText()) == "CAR KIT" :
            if str(self.ComboBox_1.currentText()) == "RING" :
               Tab_6 = [a_6, b_6, c_6]
               print "Voici le résultat de Tab_6 :"
               print Tab_6
 
        a_7 = [self.SpinBox_1.value()]
        b_7 = [self.SpinBox_2.value()]
        c_7 = [self.SpinBox_3.value()]
        Tab_7 = [a_7, b_7, c_7]
        if str(self.ComboBox_0.currentText()) == "RSM" :
            if str(self.ComboBox_1.currentText()) == "RING" :
               Tab_7 = [a_7, b_7, c_7]
               print "Voici le résultat de Tab_7 :"
               print Tab_7
 
 
        a_8 = [self.SpinBox_1.value()]
        b_8 = [self.SpinBox_2.value()]
        c_8 = [self.SpinBox_3.value()]
        Tab_8 = [a_8, b_8, c_8]
        if str(self.ComboBox_0.currentText()) == "HANDSET" :
            if str(self.ComboBox_1.currentText()) == "BEEP" :
               Tab_8 = [a_8, b_8, c_8]
               print "Voici le résultat de Tab_8 :"
               print Tab_8
 
        a_9 = [self.SpinBox_1.value()]
        b_9 = [self.SpinBox_2.value()]
        c_9 = [self.SpinBox_3.value()]
        Tab_9 = [a_9, b_9, c_9]
        if str(self.ComboBox_0.currentText()) == "HANDFREE" :
            if str(self.ComboBox_1.currentText()) == "BEEP" :
               Tab_9 = [a_9, b_9, c_9]
               print "Voici le résultat de Tab_9 :"
               print Tab_9
 
        a_10 = [self.SpinBox_1.value()]
        b_10 = [self.SpinBox_2.value()]
        c_10 = [self.SpinBox_3.value()]
        Tab_10 = [a_10, b_10, c_10]
        if str(self.ComboBox_0.currentText()) == "CAR KIT" :
            if str(self.ComboBox_1.currentText()) == "BEEP" :
               Tab_10 = [a_10, b_10, c_10]
               print "Voici le résultat de Tab_10 :"
               print Tab_10
 
        a_11 = [self.SpinBox_1.value()]
        b_11 = [self.SpinBox_2.value()]
        c_11 = [self.SpinBox_3.value()]
        Tab_11 = [a_11, b_11, c_11]
        if str(self.ComboBox_0.currentText()) == "RSM" :
            if str(self.ComboBox_1.currentText()) == "BEEP" :
               Tab_11 = [a_11, b_11, c_11]
               print "Voici le résultat de Tab_11 :"
               print Tab_11
#-----------------------------
 
        self.SpinBox_1.valueChanged.connect(self.on_spinbox_changed)
        self.SpinBox_2.valueChanged.connect(self.on_spinbox_changed)
        self.SpinBox_3.valueChanged.connect(self.on_spinbox_changed)
 
    def on_spinbox_changed(self, idx):
 
        a = [self.SpinBox_1.value()]
        b = [self.SpinBox_2.value()]
        c = [self.SpinBox_3.value()]
        Tab_0 = [a, b, c]
        if str(self.ComboBox_0.currentText()) == "HANDSET" :
            if str(self.ComboBox_1.currentText()) == "VOICE" :
               Tab = [a, b, c]
               print "Voici le résultat de Tab_0 :"
               print Tab_0
 
        a_1 = [self.SpinBox_1.value()]
        b_1 = [self.SpinBox_2.value()]
        c_1 = [self.SpinBox_3.value()]
        Tab_1 = [a_1, b_1, c_1]
        if str(self.ComboBox_0.currentText()) == "HANDFREE" :
            if str(self.ComboBox_1.currentText()) == "VOICE" :
               Tab_1 = [a_1, b_1, c_1]
               print "Voici le résultat de Tab_1 :"
               print Tab_1
 
 
        a_2 = [self.SpinBox_1.value()]
        b_2 = [self.SpinBox_2.value()]
        c_2 = [self.SpinBox_3.value()]
        Tab_2 = [a_2, b_2, c_2]
        if str(self.ComboBox_0.currentText()) == "CAR KIT" :
            if str(self.ComboBox_1.currentText()) == "VOICE" :
               Tab_2 = [a_2, b_2, c_2]
               print "Voici le résultat de Tab_2 :"
               print Tab_2
 
        a_3 = [self.SpinBox_1.value()]
        b_3 = [self.SpinBox_2.value()]
        c_3 = [self.SpinBox_3.value()]
        Tab_3 = [a_3, b_3, c_3]
        if str(self.ComboBox_0.currentText()) == "RSM" :
            if str(self.ComboBox_1.currentText()) == "VOICE" :
               Tab_3 = [a_3, b_3, c_3]
               print "Voici le résultat de Tab_3 :"
               print Tab_3
 
        a_4 = [self.SpinBox_1.value()]
        b_4 = [self.SpinBox_2.value()]
        c_4 = [self.SpinBox_3.value()]
        Tab_4 = [a_4, b_4, c_4]
        if str(self.ComboBox_0.currentText()) == "HANDSET" :
            if str(self.ComboBox_1.currentText()) == "RING" :
               Tab_4 = [a_4, b_4, c_4]
               print "Voici le résultat de Tab_4 :"
               print Tab_4
 
        a_5 = [self.SpinBox_1.value()]
        b_5 = [self.SpinBox_2.value()]
        c_5 = [self.SpinBox_3.value()]
        Tab_5 = [a_5, b_5, c_5]
        if str(self.ComboBox_0.currentText()) == "HANDFREE" :
            if str(self.ComboBox_1.currentText()) == "RING" :
               Tab_5 = [a_5, b_5, c_5]
               print "Voici le résultat de Tab_5 :" 
               print Tab_5
 
        a_6 = [self.SpinBox_1.value()]
        b_6 = [self.SpinBox_2.value()]
        c_6 = [self.SpinBox_3.value()]
        Tab_6 = [a_6, b_6, c_6]
        if str(self.ComboBox_0.currentText()) == "CAR KIT" :
            if str(self.ComboBox_1.currentText()) == "RING" :
               Tab_6 = [a_6, b_6, c_6]
               print "Voici le résultat de Tab_6 :"
               print Tab_6
 
        a_7 = [self.SpinBox_1.value()]
        b_7 = [self.SpinBox_2.value()]
        c_7 = [self.SpinBox_3.value()]
        Tab_7 = [a_7, b_7, c_7]
        if str(self.ComboBox_0.currentText()) == "RSM" :
            if str(self.ComboBox_1.currentText()) == "RING" :
               Tab_7 = [a_7, b_7, c_7]
               print "Voici le résultat de Tab_7 :"
               print Tab_7
 
        a_8 = [self.SpinBox_1.value()]
        b_8 = [self.SpinBox_2.value()]
        c_8 = [self.SpinBox_3.value()]
        Tab_8 = [a_8, b_8, c_8]
        if str(self.ComboBox_0.currentText()) == "HANDSET" :
            if str(self.ComboBox_1.currentText()) == "BEEP" :
               Tab_8 = [a_8, b_8, c_8]
               print "Voici le résultat de Tab_8 :"
               print Tab_8
 
        a_9 = [self.SpinBox_1.value()]
        b_9 = [self.SpinBox_2.value()]
        c_9 = [self.SpinBox_3.value()]
        Tab_9 = [a_9, b_9, c_9]
        if str(self.ComboBox_0.currentText()) == "HANDFREE" :
            if str(self.ComboBox_1.currentText()) == "BEEP" :
               Tab_9 = [a_9, b_9, c_9]
               print "Voici le résultat de Tab_9 :"
               print Tab_9
 
        a_10 = [self.SpinBox_1.value()]
        b_10 = [self.SpinBox_2.value()]
        c_10 = [self.SpinBox_3.value()]
        Tab_10 = [a_10, b_10, c_10]
        if str(self.ComboBox_0.currentText()) == "CAR KIT" :
            if str(self.ComboBox_1.currentText()) == "BEEP" :
               Tab_10 = [a_10, b_10, c_10]
               print "Voici le résultat de Tab_10 :"
               print Tab_10
 
        a_11 = [self.SpinBox_1.value()]
        b_11 = [self.SpinBox_2.value()]
        c_11 = [self.SpinBox_3.value()]
        Tab_11 = [a_11, b_11, c_11]
 
        if str(self.ComboBox_0.currentText()) == "RSM" :
            if str(self.ComboBox_1.currentText()) == "BEEP" :
               Tab_11 = [a_11, b_11, c_11]
               print "Voici le résultat de Tab_11 :"
               print Tab_11
#--------------------------------------------
 
    def M_Action1(self):
 
        with open("C:/Users/ssrinivasa/Desktop/texto.txt") as f:
            x = int(f.readline().rstrip())                     
            self.SpinBox_1.setValue(x)       
            x = int(f.readline().rstrip())
            self.SpinBox_2.setValue(x)
            x = int(f.readline().rstrip())
            self.SpinBox_3.setValue(x)
            x = int(f.readline().rstrip())
 
#--------------------------------------------
    def M_Action2(self):
 
        reponse = self.SpinBox_1.value()
        fichier = open("C:/Users/ssrinivasa/Desktop/fichier.txt", "a")
        fichier.write(str(reponse) +"\r\n") # ajouter nouvelle ligne après écriture
        reponse = self.SpinBox_2.value()
        fichier.write(str(reponse) +"\r\n")
        reponse = self.SpinBox_3.value()        
        fichier.write(str(reponse) +"\r\n")        
        print reponse
#-------------------------------------------
    def M_Action3(self):
        self.Serial_Write()
 
    def Serial_Write(self):
        value = str(self.SpinBox_1.value())
        F_Serial(2,value,"null")
        value = str(self.SpinBox_2.value())
        F_Serial(2,value,"null")
        value = str(self.SpinBox_3.value())
        F_Serial(2,value,"null")
 
#--------------------------------------------
    def F_Popup(self):
        self.w = MyPopup()
        self.w.setGeometry(QtCore.QRect(100, 100, 400, 200))
        self.w.show()
#--------------------------------------------
 
def main():
        '''Application'''
        App = QtGui.QApplication(sys.argv)
        App.setStyle("windows")
        MainWin2 = essai()
        MainWin2.show()
        editor = essai()
        sys.exit(App.exec_() )
 
if __name__=='__main__':
    main()
Merci d'avance.