1import sys
2from PyQt4.QtCore import *
3from PyQt4.QtGui import *
4
5class Form(QDialog):
6 def __init__(self, parent=None):
7 super(Form, self).__init__(parent)
8
9 layout = QVBoxLayout()
10 self.b1 = QPushButton("Button1")
11 self.b1.setCheckable(True)
12 self.b1.toggle()
13 self.b1.clicked.connect(lambda:self.whichbtn(self.b1))
14 self.b1.clicked.connect(self.btnstate)
15 layout.addWidget(self.b1)
16
17 self.b2 = QPushButton()
18 self.b2.setIcon(QIcon(QPixmap("python.gif")))
19 self.b2.clicked.connect(lambda:self.whichbtn(self.b2))
20 layout.addWidget(self.b2)
21 self.setLayout(layout)
22 self.b3 = QPushButton("Disabled")
23 self.b3.setEnabled(False)
24 layout.addWidget(self.b3)
25
26 self.b4 = QPushButton("&Default")
27 self.b4.setDefault(True)
28 self.b4.clicked.connect(lambda:self.whichbtn(self.b4))
29 layout.addWidget(self.b4)
30
31 self.setWindowTitle("Button demo")
32
33 def btnstate(self):
34 if self.b1.isChecked():
35 print "button pressed"
36 else:
37 print "button released"
38
39 def whichbtn(self,b):
40 print "clicked button is "+b.text()
41
42def main():
43 app = QApplication(sys.argv)
44 ex = Form()
45 ex.show()
46 sys.exit(app.exec_())
47
48if __name__ == '__main__':
49 main()