按钮QPushButton
常用方法 | 描述 |
---|---|
setCheckable() | 设置按钮是否已经被选中,如果设置True,则表示按钮将保持已点击和释放状态 |
toggle() | 在按钮状态之间进行切换 |
setIcon() | 设置按钮上的图标 |
setEnabled() | 设置按钮是否可以使用,当设置为False时,按钮变成不可用状态,点击它不会发射信号 |
isChecked() | 返回按钮的状态,返回值为True或者False |
setDefault() | 设置按钮的默认状态 |
setText() | 设置按钮的显示文本 |
text() | 返回按钮的显示文本 |
设置快捷键
通过按钮名字能为QPushButton设置快捷键,比如名字为‘&Download’的按键,它的快捷键是‘Alt+D’。其规则是;想要实现快捷键为“Alt+D”,那么按钮的名字里有D这个字母,并且在D的前面加上“&”,这个字母D一般是按钮名称的首字母,而且在按钮显示时。“&”不会显示出来,如果想显示,那么需要转义,核心代码如下
# -*- 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")
self.btn1.setCheckable(True)
self.btn1.toggle()
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")
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
方法 | 描述 |
---|---|
setCheckanle() | 设置按钮是否已经被选中,可以改变单选按钮的选中状态,如果设置为True则表示单选按钮将保持以点击和释放状态 |
isChecked() | 返回单选按钮的状态,返回值True或False |
setText() | 设置单选按钮显示的文本 |
text() | 返回单选按钮显示的文本 |
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)#将按钮1设为默认选中
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_())
self.btn1.setChecked(True)#将按钮1设为默认选中
self.btn1.toggled.connect(lambda: self.btnstate(self.btn1))
toggled状态之间切换
复选框按钮QCheckBox
方法 | 描述 |
---|---|
setChecked() | 设置复选框的状态,设置为True表示选中,False表示取消选中的复选框 |
setText() | 设置复选框的显示文本 |
text() | 返回复选框的显示文本 |
isChecked() | 检查复选框是否被选中 |
setTriState() | 设置复选框为一个三态复选框 |
setCheckState() | 三态复选框的状态设置,具体设置可以见下表 |
三态复选框的状态设置
名称 | 值 和含义 |
---|---|
Qt.Checked | 2 , 组件没有被选中(默认) |
Qt.PartiallyChecked | 1,组件被半选中 |
Qt.Unchecked | 0,组件被选中 |
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组
groupBox = QGroupBox("Checkboxes")
groupBox.setFlat(False)
#创建复选框1,并默认选中,当状态改变时信号触发事件
self.checkBox1 = QCheckBox("&Checkbox1")
self.checkBox1.setChecked(True)
self.checkBox1.stateChanged.connect(lambda: self.btnstate(self.checkBox1))
#创建复选框,标记状态改变时信号触发事件
self.checkBox2 = QCheckBox("Checkbox2")
self.checkBox2.toggled.connect(lambda: self.btnstate(self.checkBox2))
#创建复选框3,设置为3状态,设置默认选中状态为半选状态,当状态改变时信号触发事件
self.checkBox3 = QCheckBox("tristateBox")
self.checkBox3.setTristate(True)
self.checkBox3.setCheckState(Qt.PartiallyChecked)
self.checkBox3.stateChanged.connect(lambda: self.btnstate(self.checkBox3))
#水平布局
layout = QHBoxLayout()
#控件添加到水平布局中
layout.addWidget(self.checkBox1)
layout.addWidget(self.checkBox2)
layout.addWidget(self.checkBox3)
#设置QGroupBox组的布局方式
groupBox.setLayout(layout)
#设置主界面布局垂直布局
mainLayout = QVBoxLayout()
#QgroupBox的控件添加到主界面布局中
mainLayout.addWidget(groupBox)
#设置主界面布局
self.setLayout(mainLayout)
#设置主界面标题
self.setWindowTitle("checkbox demo")
#输出三个复选框当前的状态,0选中,1半选,2没选中
def btnstate(self, btn):
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_())
QGroupBox为构建分组框提供了支持。分组框通常带有一个边框和一个标题栏,作为容器部件来使用,在其中可以布置各种窗口部件。布局时可用作一组控件的容器,
电气专业的计算机萌新,写博文不容易,如果你觉得本文对你有用,请点个赞支持下。谢谢。