PyQt5中的按钮类控件

488 阅读2分钟

小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。

软硬件环境

  • Windows 10 64bit
  • Anaconda3 with python 3.7
  • PyCharm 2020.1

简介

在任何GUI设计中,按钮都是最重要的触发请求动作的方式,用来与用户进行交互操作。在PyQt5中按钮的基类是QAbstractButton,它提供了按钮的通用功能,但是它是抽象类,不能被实例化,必须由它的子类来实现不同形式、不同功能的按钮。本文讲述三种不同形式的按钮,QPushButtonQRadioButtonQCheckBox

QPushButton

QPushButton是最常见的按钮,它的形状是长方形的。常用方法有

方法说明
setCheckable()设置按钮是否被选中
toggle()在按钮状态之间进行切换
setIcon()设置按钮上的图标
setEnabled()设置按钮是否可以使用
isChecked()返回按钮的状态
setDefault()设置按钮的默认状态
setText()设置按钮的显示文本
text()返回按钮的显示文本
import sys
from PyQt5.QtWidgets import QWidget, QCheckBox, QPushButton, QApplication


class Example(QWidget):
    
    def __init__(self):
        super().__init__()
        
        self.initUI()
    
    def initUI(self):
        pb = QPushButton('PushButton', self)
        pb.move(50, 50)
        pb.clicked.connect(self.buttonClicked)
        
        self.setWindowTitle('PushButton')
        self.show()
    
    def buttonClicked(self):
        print('Button is clicked.')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

button

QRadioButton

QRadioButton提供了一组可供选择的按钮和文本标签,用户只能选择其中之一。当按钮进行切换的时候,就会发送toggled信号。

import sys

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


class Example(QWidget):
    def __init__(self, parent=None):
        super(Example, self).__init__(parent)
        layout = QHBoxLayout()
        self.button1 = QRadioButton("Button1")
        self.button1.setChecked(True)
        self.button1.toggled.connect(lambda: self.buttonToggled(self.button1))
        layout.addWidget(self.button1)
        
        self.button2 = QRadioButton("Button2")
        self.button2.toggled.connect(lambda: self.buttonToggled(self.button2))
        layout.addWidget(self.button2)
        
        self.setLayout(layout)
        self.setWindowTitle("RadioButton")
    
    def buttonToggled(self, button):
        if button.text() == "Button1":
            if button.isChecked() == True:
                print(f"{button.text()} is selected.")
            else:
                print(f"{button.text()} is deselected.")
        
        if button.text() == "Button2":
            if button.isChecked() == True:
                print(f"{button.text()} is selected.")
            else:
                print(f"{button.text()} is deselected.")


if __name__ == '__main__':
    app = QApplication(sys.argv)
    example = Example()
    example.show()
    sys.exit(app.exec_())

button

QCheckBox

QCheckBox提供了一组带文本标签的复选框,用户可以选择多个选项。

import sys

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *


class Example(QWidget):
    
    def __init__(self):
        super().__init__()
        
        layout = QHBoxLayout()
        
        self.checkbox1 = QCheckBox("CheckBox1")
        self.checkbox1.stateChanged.connect(lambda: self.checkboxChecked(self.checkbox1))
        layout.addWidget(self.checkbox1)
        
        self.checkbox2 = QCheckBox("CheckBox2")
        self.checkbox2.stateChanged.connect(lambda: self.checkboxChecked(self.checkbox2))
        layout.addWidget(self.checkbox2)
        
        self.setLayout(layout)
        self.setWindowTitle("CheckBox")
    
    def checkboxChecked(self, cb):
        if cb.text() == "CheckBox1":
            if cb.isChecked():
                print(f"{cb.text()} is checked.")
            else:
                print(f"{cb.text()} is unchecked.")
        
        if cb.text() == "CheckBox2":
            if cb.isChecked():
                print(f"{cb.text()} is checked.")
            else:
                print(f"{cb.text()} is unchecked.")


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())

button

源码下载

github.com/xugaoxiang/…