pyqt5新手入门:开关按钮 Toggle button

开关按钮 Toggle button
ToggleButton是QPushButton的一种特殊模式。它是一个有两种状态的按钮:按下与未按下。通过点击在这两种状态间来回切换。这种功能在某些场景会很实用。

# coding: utf-8
import sys
from PyQt5.QtWidgets import (QWidget, QPushButton, 
    QFrame, QApplication)
from PyQt5.QtGui import QColor
 
 
class Example(QWidget):
    
    def __init__(self):
        super().__init__()
        self.initUI()
        
    def initUI(self):      
        self.col = QColor(0, 0, 0)       
 
        redb = QPushButton('Red', self)
        redb.setCheckable(True)
        redb.move(10, 10)
        # 当按钮被第一次按下然后释放时发出信号,并将当前状态传递给外界
        redb.clicked[bool].connect(self.setColor)
 
        greenb = QPushButton('Green', self)
        greenb.setCheckable(True)
        greenb.move(10, 60)
        greenb.clicked[bool].connect(self.setColor)
 
        blueb = QPushButton('Blue', self)
        blueb.setCheckable(True)
        blueb.move(10, 110)
        blueb.clicked[bool].connect(self.setColor)
 
        self.square = QFrame(self)
        self.square.setGeometry(150, 20, 100, 100)
        self.square.setStyleSheet("QWidget { background-color: %s }" %  
            self.col.name())
        self.setGeometry(300, 300, 280, 170)
        self.setWindowTitle('Toggle button')
        self.show()
        
    # pressed获得按钮的状态 
    def setColor(self, pressed):
        source = self.sender()
        if pressed:
            val = 255
        else: val = 0
        if source.text() == "Red":
            self.col.setRed(val)                
        elif source.text() == "Green":
            self.col.setGreen(val)           
        else:
            self.col.setBlue(val) 
        self.square.setStyleSheet("QFrame { background-color: %s }" %
            self.col.name())  
       
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
    
    

pyqt5新手入门:开关按钮 Toggle button_第1张图片

你可能感兴趣的:(pyqt5新手入门:开关按钮 Toggle button)