PyQt5 QRadioButton按钮

QRadioButton是一个单选按钮,可以打开(选中)或关闭(取消选中)。在一组单选按钮中,一次只能选中其中一个按钮。

打开或关闭按钮,都会发出toggled()信号。使用isChecked()可以查看是否选择了一个特定的按钮。

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QRadioButton, QLabel
from PyQt5.QtGui import QPixmap

class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.resize(250, 155)
        self.setWindowTitle('title')

        self.on_button = QRadioButton('ON', self)
        self.on_button.toggled.connect(self.choose)

        self.off_button = QRadioButton('OFF', self)
        self.off_button.move(0, 30)
        self.off_button.toggled.connect(self.choose)

        self.label = QLabel(self)
        self.label.setGeometry(60, 30, 100, 100)

        self.show()
    
    def choose(self):
        if self.on_button.isChecked():
            self.label.setPixmap(QPixmap('D:\png\on.png'))
        else:
            self.label.setPixmap(QPixmap('D:\png\off.png'))

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

 

posted @ 2020-01-17 15:21  PIPO2  阅读(2234)  评论(0编辑  收藏  举报