QPushButton按钮的使用
Posted leoych
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了QPushButton按钮的使用相关的知识,希望对你有一定的参考价值。
1 import sys 2 from PyQt5.QtCore import * 3 from PyQt5.QtGui import * 4 from PyQt5.QtWidgets import * 5 6 class Form(QDialog): 7 def __init__(self): 8 super(Form, self).__init__() 9 layout = QVBoxLayout() 10 11 self.btn1 = QPushButton("Button1") 12 self.btn1.setCheckable(True) 13 self.btn1.toggle() 14 self.btn1.clicked.connect(lambda: self.whichbtn(self.btn1)) 15 self.btn1.clicked.connect(self.btnstate) 16 layout.addWidget(self.btn1) 17 18 self.btn2 = QPushButton("image") 19 self.btn2.setIcon(QIcon(QPixmap("chinaz16.png"))) 20 self.btn2.clicked.connect(lambda : self.whichbtn(self.btn2)) 21 layout.addWidget(self.btn2) 22 self.setLayout(layout) 23 24 self.btn3 = QPushButton("Disabled") 25 self.btn3.setEnabled(False) 26 layout.addWidget(self.btn3) 27 28 self.btn4 = QPushButton("&Download") 29 self.btn4.setDefault(True) 30 self.btn4.clicked.connect(lambda: self.whichbtn(self.btn4)) 31 layout.addWidget(self.btn4) 32 33 self.setWindowTitle("Button demo") 34 35 def btnstate(self): 36 if self.btn.isChecked(): 37 print("button pressed") 38 else: 39 print("button released") 40 41 def whichbtn(self,btn): 42 print("clicked button is "+ btn.text()) 43 44 if __name__ == ‘__main__‘: 45 app = QApplication(sys.argv) 46 btnDemo = Form() 47 btnDemo.show() 48 sys.exit(app.exec_())
效果图:
以上是关于QPushButton按钮的使用的主要内容,如果未能解决你的问题,请参考以下文章
QT软件开发之基础控件--2.1.2 QPushButton按钮