Salut a tous,
J'aimerai avoir une simple programme qui ouvre une fenêtre popup lorsqu'on clique sur un bouton.
Ainsi si possible, j'aimerai avoir un spinbox dans la fenêtre popup qui s'ouvre.
Pouvez-vous m'aider SVP ?
Version imprimable
Salut a tous,
J'aimerai avoir une simple programme qui ouvre une fenêtre popup lorsqu'on clique sur un bouton.
Ainsi si possible, j'aimerai avoir un spinbox dans la fenêtre popup qui s'ouvre.
Pouvez-vous m'aider SVP ?
Salut,
Tu demandes quelque chose que tu sais faire:
http://www.developpez.net/forums/d14...rer-programme/
Salut merci pour ta réponse,
j'ai réussit a crée une fenêtre popup, mais comment connecter les modifications des spinbox au mainWindow ?
J'ai crée des spinbox dans le menu principal, et quand tu les modifies, sa prend en compte la combinaison des 2 combobox et enregistre le résultat dans un tableau.
Donc comment faire la même chose pour les spinbox qui sont dans le popup ?
Voici mon code pour mieux comprendre ma demande :
J'ai crée une class MyPopup_1, qui ouvre une fenêtre lorsqu'on clique sur le bouton "DRC"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
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 #!/usr/bin/env python # -*- coding: cp1252 -*- #SPEC 0.4 import sys import time import serial import binascii from PyQt4 import QtCore, QtGui Tab = [0]*12 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 2") self.ComboBox_11.addItem("COM 3") 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 MyPopup_1(QtGui.QWidget): def __init__(self): QtGui.QWidget.__init__(self) self.SpinBox_11 = QtGui.QSpinBox(self) self.SpinBox_11.setRange(-60,0) self.SpinBox_11.setSingleStep(1) self.SpinBox_11.setValue(0) self.SpinBox_22 = QtGui.QSpinBox(self) self.SpinBox_22.setRange(-96,0) self.SpinBox_22.setSingleStep(1) self.SpinBox_22.setValue(0) self.SpinBox_33 = QtGui.QSpinBox(self) self.SpinBox_33.setRange(-96,0) self.SpinBox_33.setSingleStep(1) self.SpinBox_33.setValue(0) position = QtGui.QGridLayout() position.addWidget(self.SpinBox_11, 0, 0) position.addWidget(self.SpinBox_22, 1, 0) position.addWidget(self.SpinBox_33, 2, 0) self.setLayout(position) #-------------------------------------------- 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) self.PushButton_1 = QtGui.QPushButton("DRC") self.connect(self.PushButton_1,QtCore.SIGNAL("clicked()"),self.fenetre) GridLayout_1 = QtGui.QGridLayout() GridLayout_1.addWidget(self.SpinBox_1, 1,0,1,1) GridLayout_1.addWidget(self.SpinBox_2, 2,0,1,1) GridLayout_1.addWidget(self.SpinBox_3, 3,0,1,1) GridLayout_1.addWidget(self.PushButton_1, 4,0,1,1) O_GroupBox_1 = QtGui.QGroupBox() O_GroupBox_1.setLayout(GridLayout_1) self.setCentralWidget(O_GroupBox_1) #-------------------------------------------- self.ComboBox_0.currentIndexChanged.connect(self.choix) self.ComboBox_1.currentIndexChanged.connect(self.choix) self.SpinBox_1.valueChanged.connect(self.choix) self.SpinBox_2.valueChanged.connect(self.choix) self.SpinBox_3.valueChanged.connect(self.choix) def choix(self): a = [self.SpinBox_1.value()] b = [self.SpinBox_2.value()] c = [self.SpinBox_3.value()] if str(self.ComboBox_0.currentText()) == 'HANDSET' and str(self.ComboBox_1.currentText()) == 'VOICE': Tab[0] = [a, b, c] elif str(self.ComboBox_0.currentText()) == 'HANDSET' and str(self.ComboBox_1.currentText()) == 'RING': Tab[1] = [a, b, c] elif str(self.ComboBox_0.currentText()) == 'HANDSET' and str(self.ComboBox_1.currentText()) == 'BEEP': Tab[2] = [a, b, c] elif str(self.ComboBox_0.currentText()) == 'HANDFREE' and str(self.ComboBox_1.currentText()) == 'VOICE': Tab[3] = [a, b, c] elif str(self.ComboBox_0.currentText()) == 'HANDFREE' and str(self.ComboBox_1.currentText()) == 'RING': Tab[4] = [a, b, c] elif str(self.ComboBox_0.currentText()) == 'HANDFREE' and str(self.ComboBox_1.currentText()) == 'BEEP': Tab[5] = [a, b, c] elif str(self.ComboBox_0.currentText()) == 'CAR KIT' and str(self.ComboBox_1.currentText()) == 'VOICE': Tab[6] = [a, b, c] elif str(self.ComboBox_0.currentText()) == 'CAR KIT' and str(self.ComboBox_1.currentText()) == 'RING': Tab[7] = [a, b, c] elif str(self.ComboBox_0.currentText()) == 'CAR KIT' and str(self.ComboBox_1.currentText()) == 'BEEP': Tab[8] = [a, b, c] elif str(self.ComboBox_0.currentText()) == 'RSM' and str(self.ComboBox_1.currentText()) == 'VOICE': Tab[9] = [a, b, c] elif str(self.ComboBox_0.currentText()) == 'RSM' and str(self.ComboBox_1.currentText()) == 'RING': Tab[10] = [a, b, c] elif str(self.ComboBox_0.currentText()) == 'RSM' and str(self.ComboBox_1.currentText()) == 'BEEP': Tab[11] = [a, b, c] #-------------------------------------------------------------------------------------------------------------------- if str(self.ComboBox_0.currentText()) == 'HANDSET' and str(self.ComboBox_1.currentText()) == 'VOICE': Tab[0] = [a, b, c] print "Voici le résultat de Tab_0 :" print Tab[0] elif str(self.ComboBox_0.currentText()) == 'HANDSET' and str(self.ComboBox_1.currentText()) == 'RING': Tab[1] = [a, b, c] print "Voici le résultat de Tab_1 :" print Tab[1] elif str(self.ComboBox_0.currentText()) == 'HANDSET' and str(self.ComboBox_1.currentText()) == 'BEEP': Tab[2] = [a, b, c] print "Voici le résultat de Tab_2:" print Tab[2] elif str(self.ComboBox_0.currentText()) == 'HANDFREE' and str(self.ComboBox_1.currentText()) == 'VOICE': Tab[3] = [a, b, c] print "Voici le résultat de Tab_3:" print Tab[3] elif str(self.ComboBox_0.currentText()) == 'HANDFREE' and str(self.ComboBox_1.currentText()) == 'RING': Tab[4] = [a, b, c] print "Voici le résultat de Tab_4:" print Tab[4] elif str(self.ComboBox_0.currentText()) == 'HANDFREE' and str(self.ComboBox_1.currentText()) == 'BEEP': Tab[5] = [a, b, c] print "Voici le résultat de Tab_5:" print Tab[5] elif str(self.ComboBox_0.currentText()) == 'CAR KIT' and str(self.ComboBox_1.currentText()) == 'VOICE': Tab[6] = [a, b, c] print "Voici le résultat de Tab_6:" print Tab[6] elif str(self.ComboBox_0.currentText()) == 'CAR KIT' and str(self.ComboBox_1.currentText()) == 'RING': Tab[7] = [a, b, c] print "Voici le résultat de Tab_7:" print Tab[7] elif str(self.ComboBox_0.currentText()) == 'CAR KIT' and str(self.ComboBox_1.currentText()) == 'BEEP': Tab[8] = [a, b, c] print "Voici le résultat de Tab_8:" print Tab[8] elif str(self.ComboBox_0.currentText()) == 'RSM' and str(self.ComboBox_1.currentText()) == 'VOICE': Tab[9] = [a, b, c] print "Voici le résultat de Tab_9:" print Tab[9] elif str(self.ComboBox_0.currentText()) == 'RSM' and str(self.ComboBox_1.currentText()) == 'RING': Tab[10] = [a, b, c] print "Voici le résultat de Tab_10:" print Tab[10] elif str(self.ComboBox_0.currentText()) == 'RSM' and str(self.ComboBox_1.currentText()) == 'BEEP': Tab[11] = [a, b, c] print "Voici le résultat de Tab_11:" print Tab[11] def initialisation(self): Tab = [0]*12 #-------------------------------------------- def fenetre(self): print "Opening a new popup window..." self.w = MyPopup_1() self.w.setGeometry(QtCore.QRect(100, 100, 400, 200)) self.w.show() #-------------------------------------------- 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()
Hesitez pas a revenir vers moi si vous avez pas comprit :)
Ton code me hérisse le poil.
Avant de t'expliquer comment transférer des données entre une appli et un dialogue, je pense utile de te faire connaître quelques conventions d'écriture communément admises en Python.
Les triples guillemets ne servent pas aux commentaires.
Ça pas bien:
Ça meilleur:Code:
1
2''' Titre_1 '''
Les noms des classes sont capitalisés et utilisent la camelCase, les noms de fonctions ne sont pas capitalisés et utilisent le underscore.Code:
1
2# Titre_1
Ça pas bien
Ça meilleur:Code:
1
2
3
4 class essai ... def F_Open
Cela peut paraître des futilités mais ce sont ces petites choses qui font que l'on aie envie de lire ton code ou pas.Code:
1
2
3
4 class Essai ... def f_open
Pour répondre à ta question, ce que tu appelles un popup est en fait un dialogue, il ne sert pas à annoncer que la soupe est servie mais à échanger des données avec l'utilisateur.
On crée un dialogue comme ceci:
Ensuite le dialogue:Code:
1
2
3
4
5
6
7
8
9
10 def f_popup(self): data = ["COM 1", "4800"] # On passe cette liste à notre dialogue self.w = MyPopup(data) rep = self.w.exec_() if rep: F_Serial(1, data[0], data[1]) else: F_Serial(4, "null", "null")
Tu remarqueras le signal des comboBox currentIndexChanged[QtCore.QString], lorsque, dans la doc, il existe plusieurs types de données transmises pour un même signal, comme ici: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 class MyPopup(QtGui.QDialog): def __init__(self, settings): QtGui.QDialog.__init__(self) self.settings = settings posit = QtGui.QVBoxLayout(self) self.label = QtGui.QLabel() self.label.setText("NUMERO DE PORT") self.label_1 = QtGui.QLabel() self.label_1.setText("VITESSE") self.combo = QtGui.QComboBox() self.combo.addItems(["COM 1", "COM 2", "COM 3", "COM 4"]) self.combo_1 = QtGui.QComboBox() self.combo_1.addItems(["4800", "115200"]) self.bouton = QtGui.QPushButton("Open", self) self.bouton_1 = QtGui.QPushButton("Close", self) posit.addWidget(self.label) posit.addWidget(self.combo) posit.addWidget(self.label_1) posit.addWidget(self.combo_1) posit.addWidget(self.bouton) posit.addWidget(self.bouton_1) self.bouton.clicked.connect(self.open) self.bouton_1.clicked.connect(self.close) self.combo.currentIndexChanged[QtCore.QString].connect(self.on_port_changed) self.combo_1.currentIndexChanged[QtCore.QString].connect(self.on_rate_changed) def on_port_changed(self, port): self.settings[0] = str(port) def on_rate_changed(self, rate): self.settings[1] = str(rate) def open(self): self.accept() def close(self): self.reject()
le premier est le signal par défaut et pour obtenir la variante QString il suffit de l'indiquer entre crochets comme dans le code ci-dessus.Citation:
void currentIndexChanged (int)
void currentIndexChanged (const QString&)
D'autres remarques sur ton code:
Tu crées deux fois de suite le même objet, lignes 144 et 156:
Plutôt que de répéter vingt foisCode:
1
2 menubar = self.menuBar()
il serait plus simple de lire le contenu des combos une seule fois.Code:
1
2 if str(self.ComboBox_0.currentText()) == 'HANDSET' and str(self.ComboBox_1.currentText()) == 'VOICE':
Ce sera tout pour aujourd'hui.Code:
1
2
3
4
5 device = str(self.ComboBox_0.currentText()) mode = str(self.ComboBox_1.currentText()) if device == 'HANDSET' and mode == 'VOICE': ...
Merci beaucoup pour ta réponse.
En effet tu crée tes combobox, bouton, label... Dans le fenêtre Popup.
Mais le problème est que mes Combo box sont dans le MainWindow, et que je doit connecter les Spinbox qui sont dans ma fenêtre popup(lorsque je clique sur le bouton "DRC") au combobox qui sont dans le MainWindow.
Ainsi j'utiliserai donc la fonction "def (choix)" dans lequel contient les phases de test, et d'affichage dans le shell.
Remarque : la fonctions "def (choix)", est construit dans une autre class.
Tu peut m'aider stp ?
Merci d'avance
Ha oui, c'est l'autre popup.
Ça ne change rien, c'est aussi un dialogue, donc transpose exactement les mêmes changements dans cette classe là.
Comme ceci:
Le print te montrera que les nouvelles données des spinBox sont bien dans la liste data.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 class MyPopup_1(QtGui.QDialog): def __init__(self, settings): QtGui.QDialog.__init__(self) self.settings = settings # ... tes spinbox ici # Tu les connectes self.SpinBox_11.valueChanged.connect(self.on_spin_changed) self.SpinBox_22.valueChanged.connect(self.on_spin_2_changed) # etc... # Ensuite tes slots def on_spin_changed(self, value): self.settings[0] = value def on_spin_1_changed(self, value): self.settings[1] = value # etc... # Et tu appelles ce dialogue exactement comme pour l'autre def fenetre(self): print "Opening a new popup window..." data = [0, 0, 0] self.w = MyPopup_1(data) rep = self.w.exec_() # On confirme que cela fonctionne comme attendu print data
Autre chose, remplace les 98 horribles lignes de la fonction choix par ceci:
Code:
1
2
3
4
5
6
7
8
9
10 def choix(self): a = self.SpinBox_1.value() b = self.SpinBox_2.value() c = self.SpinBox_3.value() device = self.ComboBox_0.currentIndex() mode = self.ComboBox_1.currentIndex() index = device * 3 + mode Tab[index] = [a, b, c] print Tab
Re Salut,
Je ne comprend pas comment tu fait une liaison avec les combo box qui sont dans la classe "essai", avec les spinbox de la classe "MyPopup_1"
Tu peut m'expliquer à quoi correspond :
Et aussi : data = [0, 0, 0]Code:
1
2 def on_spin_changed(self, value): self.settings[0] = value
Voici la classe "MyPopup_1" que j'ai modifier :
Enfin j'ai un erreur, avec "def __init__(self,settings):"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
47 class MyPopup_1(QtGui.QDialog): def __init__(self,settings): QtGui.QDialog.__init__(self) self.settings = settings self.SpinBox_11 = QtGui.QSpinBox(self) self.SpinBox_11.setRange(-60,0) self.SpinBox_11.setSingleStep(1) self.SpinBox_11.setValue(0) self.SpinBox_22 = QtGui.QSpinBox(self) self.SpinBox_22.setRange(-96,0) self.SpinBox_22.setSingleStep(1) self.SpinBox_22.setValue(0) self.SpinBox_33 = QtGui.QSpinBox(self) self.SpinBox_33.setRange(-96,0) self.SpinBox_33.setSingleStep(1) self.SpinBox_33.setValue(0) position = QtGui.QGridLayout() position.addWidget(self.SpinBox_11, 0, 0) position.addWidget(self.SpinBox_22, 1, 0) position.addWidget(self.SpinBox_33, 2, 0) self.setLayout(position) self.SpinBox_11.valueChanged.connect(self.on_spin_changed) self.SpinBox_22.valueChanged.connect(self.on_spin_1_changed) self.SpinBox_33.valueChanged.connect(self.on_spin_2_changed) def on_spin_changed(self, value): self.settings[0] = value def on_spin_1_changed(self, value): self.settings[1] = value def on_spin_2_changed(self, value): self.settings[2] = value def fenetre(self): print "Opening a new popup window..." data = [0, 0, 0] self.w = MyPopup_1(data) rep = self.w.exec_() # On confirme que cela fonctionne comme attendu print data
Voici l'erreur :
self.w = MyPopup_1()
TypeError: __init__() takes exactly 2 arguments (1 given)
Merci pour ton aide.
Meuh non, hein, cette fonction
sert à appeler le dialogue, elle ne peut pas se trouver dans la même classe.Code:
1
2
3
4
5
6
7
8 def fenetre(self): print "Opening a new popup window..." data = [0, 0, 0] self.w = MyPopup_1(data) rep = self.w.exec_() # On confirme que cela fonctionne comme attendu print data
Tu dois changer le code de la fonction fenetre() qui existe déjà dans ta classe principale, par ce code ci.
La liste data sert à récupérer les valeurs des spinBox du dialogue.
Re re salut,
J'ai adapter tes concepts, sur mon programme. J'ai réussit a faire communiquer le fenêtre pop avec les combobox du MainWindow.
Mais le problème est que je doit fixé en amant la combinaison des combobox, je ne peut donc pas les modifier avec la fenêtre popup ouvert.
Je doit donc fermer la fenêtre popup, mettre une autre combinaison de combobox, et relancer la fenêtre popup -__-
En gros : Je veut modifier la combinaison des combobox, et communiquer avec la fenêtre popup simultanément
Peut-tu m'aider, a corriger ce problème ?
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
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 #!/usr/bin/env python # -*- coding: cp1252 -*- #SPEC 0.4 import sys import time import serial import binascii from PyQt4 import QtCore, QtGui Tab = [0]*12 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 2") self.ComboBox_11.addItem("COM 3") 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 MyPopup_1(QtGui.QWidget): def __init__(self,mode,signal): QtGui.QWidget.__init__(self) self.mode1 = mode self.signal1 = signal self.SpinBox_11 = QtGui.QSpinBox(self) self.SpinBox_11.setRange(-60,0) self.SpinBox_11.setSingleStep(1) self.SpinBox_11.setValue(0) self.SpinBox_22 = QtGui.QSpinBox(self) self.SpinBox_22.setRange(-96,0) self.SpinBox_22.setSingleStep(1) self.SpinBox_22.setValue(0) self.SpinBox_33 = QtGui.QSpinBox(self) self.SpinBox_33.setRange(-96,0) self.SpinBox_33.setSingleStep(1) self.SpinBox_33.setValue(0) position = QtGui.QGridLayout() position.addWidget(self.SpinBox_11, 0, 0) position.addWidget(self.SpinBox_22, 1, 0) position.addWidget(self.SpinBox_33, 2, 0) self.setLayout(position) self.SpinBox_11.valueChanged.connect(self.test) self.SpinBox_22.valueChanged.connect(self.test) self.SpinBox_33.valueChanged.connect(self.test) def test(self): a = [self.SpinBox_11.value()] b = [self.SpinBox_22.value()] c = [self.SpinBox_33.value()] if self.mode1 == 'HANDSET' and self.signal1 == 'VOICE': Tab[0] = [a, b, c] print "Voici le résultat de Tab_0 :" print Tab[0] elif self.mode1 == 'HANDSET' and self.signal1 == 'RING': Tab[1] = [a, b, c] print "Voici le résultat de Tab_1 :" print Tab[1] elif self.mode1 == 'HANDSET' and self.signal1 == 'BEEP': Tab[2] = [a, b, c] print "Voici le résultat de Tab_2:" print Tab[2] elif self.mode1 == 'HANDFREE' and self.signal1 == 'VOICE': Tab[3] = [a, b, c] print "Voici le résultat de Tab_3:" print Tab[3] elif self.mode1 == 'HANDFREE' and self.signal1 == 'RING': Tab[4] = [a, b, c] print "Voici le résultat de Tab_4:" print Tab[4] elif self.mode1 == 'HANDFREE' and self.signal1 == 'BEEP': Tab[5] = [a, b, c] print "Voici le résultat de Tab_5:" print Tab[5] elif self.mode1 == 'CAR KIT' and self.signal1 == 'VOICE': Tab[6] = [a, b, c] print "Voici le résultat de Tab_6:" print Tab[6] elif self.mode1 == 'CAR KIT' and self.signal1 == 'RING': Tab[7] = [a, b, c] print "Voici le résultat de Tab_7:" print Tab[7] elif self.mode1 == 'CAR KIT' and self.signal1 == 'BEEP': Tab[8] = [a, b, c] print "Voici le résultat de Tab_8:" print Tab[8] elif self.mode1 == 'RSM' and self.signal1 == 'VOICE': Tab[9] = [a, b, c] print "Voici le résultat de Tab_9:" print Tab[9] elif self.mode1 == 'RSM' and self.signal1 == 'RING': Tab[10] = [a, b, c] print "Voici le résultat de Tab_10:" print Tab[10] elif self.mode1 == 'RSM' and self.signal1 == 'BEEP': Tab[11] = [a, b, c] print "Voici le résultat de Tab_11:" print Tab[11] #-------------------------------------------- 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") self.mode = str(self.ComboBox_0.currentText()) self.signal = str(self.ComboBox_1.currentText()) 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.PushButton_1 = QtGui.QPushButton("DRC") self.connect(self.PushButton_1,QtCore.SIGNAL("clicked()"),self.fenetre) GridLayout_1 = QtGui.QGridLayout() GridLayout_1.addWidget(self.PushButton_1, 4,0,1,1) O_GroupBox_1 = QtGui.QGroupBox() O_GroupBox_1.setLayout(GridLayout_1) self.setCentralWidget(O_GroupBox_1) #-------------------------------------------- self.ComboBox_0.currentIndexChanged.connect(self.fenetre) self.ComboBox_1.currentIndexChanged.connect(self.fenetre) def choix(self): self.mode = str(self.ComboBox_0.currentText()) self.signal = str(self.ComboBox_1.currentText() def initialisation(self): Tab = [0]*12 #-------------------------------------------- def fenetre(self): print "Opening a new popup window..." mode = self.mode signal = self.signal self.w = MyPopup_1(mode, signal) self.w.setGeometry(QtCore.QRect(100, 100, 400, 200)) self.w.show() 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()
Je ne comprend plus, pourquoi les mets-tu dans un dialogue, tes spinBox ?
Pourquoi ne pas les mettre dans la fenêtre principale si il sont en interaction avec les autres widgets ?
À quoi sert exactement Tab ? je ne vois pas à quoi tu l'utilise dans ton code.
Et remplace ta fonction test() comme indiqué dans mon post n° 7
Edit: Une question: est-ce que tu as déjà utilisé le designer de Qt ?
Merci pour ta réponse,
Le but 1er est d'externaliser les spinbox, je ne veut plus les voir sur le MainWindow.
Ainsi les spinbox communiques avec les combo box qui sont dans le MainWindow.
Dans un 2eme temps, Tab sert a enregistré les valeurs des spinbox dans une liste
La fonction test() que t'a dit, n'est plus valable. Car j'ai supprimer les spinbox1,2 et 3 qui était dans le MainWindow.
Remarque : A la fin de mon projet, je me retrouve avec 20 bouton qui ont des fonction différente. Ainsi mettre tout les contenues dans le MainWindow n'est pas une bonne idée.
Mon Problème : Je doit fermer la fenêtre popup, mettre une autre combinaison de combobox, et relancer la fenêtre popup
Edit : Qt designer n'est pas un logiciel qui permet apprendre la bonne programmation a mon regard.
Merci de m'aider SVP
Ta fenêtre contenait 3 spinBox et rien de plus, et tu me dis que tu veux les enlever de là, à quoi sert alors cette fenêtre principale ?
Les comboBox, elles, sont dans un dockWidget, pas dans la fenêtre de l'appli.
Cette débauche fantaisiste de widgets inutiles ne t'aide absolument pas, ton code pourrait être fonctionnel en très peu de temps si tu évitais cette approche irrationnelle.
Déjà, je me demande si vous avez exactement compris la nécessité et l'utilisation de cette application, je dis vous en parlant de toi et tes collègues de cours qui ont posté sur ce forum pour ce même exercice.
Aucun d'entre vous n'a été capable de d'expliquer en termes clairs à quoi servait cette application.
D'accord mais ce n'était pas ma question, tu n'utilises cette liste Tab nulle part après l'avoir remplie. Quelle est son utilité et que représentent les valeurs qui y sont mises ?Citation:
Dans un 2eme temps, Tab sert a enregistré les valeurs des spinbox dans une liste
Elle est toujours dans ton dernier code, tu l'as simplement déplacée.Citation:
La fonction test() que t'a dit, n'est plus valable. Car j'ai supprimer les spinbox1,2 et 3 qui était dans le MainWindow.
La fin de ton projet est une vue de l'esprit à ce stade ci et le nombre de boutons que peut contenir une fenêtre n'est pas un problème dont tu as à te préoccuper en ce moment.Citation:
Remarque : A la fin de mon projet, je me retrouve avec 20 bouton qui ont des fonction différente. Ainsi mettre tout les contenues dans le MainWindow n'est pas une bonne idée.
Je n'en parle pas en tant que programme d'apprentissage, je voulais juste te proposer de moins perdre ton temps en montages aussi inutiles que contre-ergonomiques.Citation:
Edit : Qt designer n'est pas un logiciel qui permet apprendre la bonne programmation a mon regard.
Il faut que tu t'aides toi-même d'abord, commence par créer un code qui soit fonctionnel, qui réalise exactement tout ce qui lui est demandé, après il sera toujours temps de s'occuper du cosmétique de l'interface.Citation:
Merci de m'aider SVP