Bonjour,
Je teste la création de boutons avec PyQt5.
Les boutons créés directement fonctionnent alors que celui créé par l'intermédiaire d'une méthode ne fonctionne pas.
Pourriez-vous me dire pourquoi?
Je joins le code ci-dessous:
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 import sys from PyQt5.QtWidgets import QApplication, QWidget, QPushButton from PyQt5.QtGui import QIcon from PyQt5.QtCore import pyqtSlot class App(QWidget): def __init__(self): super().__init__() self.title = 'PyQt5 button - pythonspot.com' self.left = 100 self.top = 100 self.width = 320 self.height = 200 self.initUI() def initUI(self): self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) btn1 = QPushButton('btn1', self) btn1.setToolTip('exemple de bouton') btn1.move(100,70) btn1.clicked.connect(self.on_click) btn2 = QPushButton('btn2', self) btn2.setToolTip('btn2') btn2.move(150,20) btn2.clicked.connect(self.on_click) browseButton = self.createButton("btn3", self.on_click) browseButton.show() self.show() def createButton(self, text, member=None): bouton = QPushButton(text) bouton.setToolTip(text) bouton.move(150,90) if (member != None) : bouton.clicked.connect(member); return bouton @pyqtSlot() def on_click(self): print('PyQt5 button click') if __name__ == '__main__': app = QApplication(sys.argv) ex = App() sys.exit(app.exec_()) else: # app = QApplication(sys.argv) ex = App()