PyQt5基礎學習-QRadioButton單選按鈕 1.QRadioButton().setChecked(設置起始狀態) 2.QRadioButton().toggled.connect(連接狀態變化時的函數) 3.self.sender(獲得當前的焦點變化按鈕)


使用QRadioButton().toggled.connect連接需要變化的函數,在函數中通過判斷單選框狀態()來self.sender().isChecked()進行變化

QRadioButtonDemo.py 

"""

單選按鈕控件(QRadioButton)

"""

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

class QRadioButtonDemo(QWidget):
    def __init__(self):
        super(QRadioButtonDemo, self).__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle("QRadioButton")
        layout = QHBoxLayout()
        self.button1 = QRadioButton("單選按鈕1")
        self.button1.setChecked(True)

        self.button1.toggled.connect(self.buttonState)
        layout.addWidget(self.button1)

        self.button2 = QRadioButton("單選按鈕2")
        self.button2.toggled.connect(self.buttonState)
        layout.addWidget(self.button2)

        self.button3 = QRadioButton("單選按鈕3")
        self.button3.toggled.connect(self.buttonState)
        layout.addWidget(self.button3)

        self.setLayout(layout)
    def buttonState(self):
        radioButton = self.sender()

        if radioButton.isChecked() == True:
            print('<' + radioButton.text() + '> 被選中')
        else:
            print("<" + radioButton.text() + "> 被取消選中狀態")


if __name__ == "__main__":
    app = QApplication(sys.argv)

    main = QRadioButtonDemo()
    main.show()

    sys.exit(app.exec_())

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM