参考书籍《PyQt5 快速开发与实战》王硕 孙洋洋 著
介绍
PyQt5按钮的基类是QAbstractButton,为抽象类,不能实例化。
常见的按钮类有:QPushButton、QToolButton、QRadioButton和QCheckBox.
代码示例
QPushButton的使用
# -*- coding: utf-8 -*-
'''
【简介】
PyQt5中QButton例子
'''
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class Form(QDialog):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
layout = QVBoxLayout() #设置垂直布局
self.btn1 = QPushButton("Button1")
# setChenkable():设置按钮是否已经被选中,true表示按钮将被保持已点击和释放状态
self.btn1.setCheckable(True)
# toggle():在按钮之间进行切换
self.btn1.toggle()
# 通过lambda方式来传递额外的参数btn1,将clicked信号发送给槽函数whichbtn()
self.btn1.clicked.connect(lambda:self.whichbtn(self.btn1) )
self.btn1.clicked.connect(self.btnstate)
layout.addWidget(self.btn1)
self.btn2 = QPushButton('image')
self.btn2.setIcon(QIcon(QPixmap("./images/python.png")))
self.btn2.clicked.connect(lambda:self.whichbtn(self.btn2) )
layout.addWidget(self.btn2)
self.setLayout(layout)
self.btn3 = QPushButton("Disabled")
# setEnabled():设置按钮是否可用,false时按钮变成不可用状态,点击它不会发送信号
self.btn3.setEnabled(False)
layout.addWidget(self.btn3)
self.btn4= QPushButton("&Download")
self.btn4.setDefault(True) #设置按钮的默认状态
self.btn4.clicked.connect(lambda:self.whichbtn(self.btn4))
layout.addWidget(self.btn4)
self.setWindowTitle("Button demo")
def btnstate(self):
if self.btn1.isChecked(): #返回按钮的状态
print("button pressed" )
else:
print("button released" )
def whichbtn(self,btn):
print("clicked button is " + btn.text() )
if __name__ == '__main__':
app = QApplication(sys.argv)
btnDemo = Form()
btnDemo.show()
sys.exit(app.exec_())
效果展示
QRadioButton的使用
# -*- coding: utf-8 -*-
'''
【简介】
PyQt5中QRadio例子
'''
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class Radiodemo(QWidget):
def __init__(self, parent=None):
super(Radiodemo, self).__init__(parent)
layout = QHBoxLayout() #设置水平布局
self.btn1 = QRadioButton("Button1")
self.btn1.setChecked(True)
self.btn1.toggled.connect(lambda:self.btnstate(self.btn1))
layout.addWidget(self.btn1)
self.btn2 = QRadioButton("Button2")
self.btn2.toggled.connect(lambda:self.btnstate(self.btn2))
layout.addWidget(self.btn2)
self.setLayout(layout)
self.setWindowTitle("RadioButton demo")
def btnstate(self,btn):
if btn.text()=="Button1":
if btn.isChecked() == True:
print( btn.text() + " is selected" )
else:
print( btn.text() + " is deselected" )
if btn.text()=="Button2":
if btn.isChecked()== True :
print( btn.text() + " is selected" )
else:
print( btn.text() + " is deselected" )
if __name__ == '__main__':
app = QApplication(sys.argv)
radioDemo = Radiodemo()
radioDemo.show()
sys.exit(app.exec_())
效果展示
QCheckBox的使用
# -*- coding: utf-8 -*-
'''
【简介】
PyQt5中 QCheckBox 例子
'''
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
class CheckBoxDemo(QWidget):
def __init__(self, parent=None):
super(CheckBoxDemo , self).__init__(parent)
groupBox = QGroupBox("Checkboxes") #设置盒子框
groupBox.setFlat( True )
layout = QHBoxLayout() #设置水平布局
self.checkBox1= QCheckBox("&Checkbox1")
self.checkBox1.setChecked(True)
self.checkBox1.stateChanged.connect( lambda:self.btnstate(self.checkBox1) )
layout.addWidget(self.checkBox1)
self.checkBox2 = QCheckBox("Checkbox2")
self.checkBox2.toggled.connect( lambda:self.btnstate(self.checkBox2) )
layout.addWidget(self.checkBox2)
self.checkBox3 = QCheckBox("tristateBox")
self.checkBox3.setTristate(True) #设置三态复选框
self.checkBox3.setCheckState(Qt.PartiallyChecked ) #Qt.PartiallyChecked表示复选框被半选中
self.checkBox3.stateChanged.connect( lambda:self.btnstate(self.checkBox3) )
layout.addWidget(self.checkBox3)
groupBox.setLayout(layout)
mainLayout = QVBoxLayout() #设置垂直布局
mainLayout.addWidget(groupBox)
self.setLayout(mainLayout)
self.setWindowTitle("checkbox demo")
def btnstate(self,btn ):
#打印复选框选中状态:0表示被选中;1表示半选中;2表示没有被选中
chk1Status = self.checkBox1.text()+", isChecked="+ str( self.checkBox1.isChecked() ) + ', chekState=' + str(self.checkBox1.checkState()) +"\n"
chk2Status = self.checkBox2.text()+", isChecked="+ str( self.checkBox2.isChecked() ) + ', checkState=' + str(self.checkBox2.checkState()) +"\n"
chk3Status = self.checkBox3.text()+", isChecked="+ str( self.checkBox3.isChecked() ) + ', checkState=' + str(self.checkBox3.checkState()) +"\n"
print(chk1Status + chk2Status + chk3Status )
if __name__ == '__main__':
app = QApplication(sys.argv)
checkboxDemo = CheckBoxDemo()
checkboxDemo.show()
sys.exit(app.exec_())
效果展示
参考资源
1.《PyQt5快速开发与实战》王硕,孙洋洋
2.《PyQt5快速开发与实战》源代码下载链接