[PyQt6] PartⅡ. Working with QRadioButton
QRadioButton Method
Method | function |
---|---|
isChecked | it return a boolean value aof true if the button and the selected state |
setIcon | add icon for the radio button, also setText which used for setting the text on the radio button |
toggle | used whenever the radio button changes its from checked to unchecked and vice versa |
click | this signal is emitted when a button is actived. its pressed and released or when its shortcut key is pressed |
state changed signal | emitted when a radio button changes its state from checked to unchecked or vice versa |
QradioButton会根据布局进行分类,注意,如果想化为数个组,不要全局布局
创建一个单选框,选择了的内容会在label上显示
from PyQt6.QtWidgets import QApplication, QWidget, QHBoxLayout, QRadioButton, QLabel, QVBoxLayout
from PyQt6.QtGui import QIcon, QFont
from PyQt6.QtCore import QSize
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(200, 200, 700, 400) # 设置窗口大小
self.setWindowTitle("Python GUI Development")
self.setWindowIcon(QIcon('python.png')) # 设置图片,没有的话不显示
self.create_radio() # 构造函数调用,显示在主窗口上
def create_radio(self):
hbox = QHBoxLayout() # 创建水平布局
rad1 = QRadioButton("Python") # 创建一个radiobutton对象,rad1
rad1.setIcon(QIcon("python.png"))
rad1.setIconSize(QSize(40, 40))
rad1.setFont(QFont("Times", 14))
rad1.setChecked(True)
rad1.toggled.connect(self.radio_selected)
rad2 = QRadioButton("Java") # 创建一个radiobutton对象,rad2
rad2.setIcon(QIcon("sid.jpg"))
rad2.setIconSize(QSize(40, 40))
rad2.setFont(QFont("Times", 14))
# rad2.setChecked(True)
rad2.toggled.connect(self.radio_selected)
rad3 = QRadioButton("JavaScript") # 创建一个radiobutton对象,rad2
rad3.setIcon(QIcon("js.jpg"))
rad3.setIconSize(QSize(40, 40))
rad3.setFont(QFont("Times", 14))
# rad3.setChecked(True)
rad3.toggled.connect(self.radio_selected)
self.label = QLabel("You have selected: Python") # 创建一个QLabel对象
self.label.setFont(QFont("Sanserif", 15)) # 设置文字样式
vbox = QVBoxLayout() # 创建一个QVBoxLayout对象
vbox.addWidget(self.label) # 在vbox中添加label
vbox.addLayout(hbox) # 将hbox布局,添加到vbox布局中
hbox.addWidget(rad1) # 水平布局对象,绑定rad1
hbox.addWidget(rad2)
hbox.addWidget(rad3)
self.setLayout(vbox) # 将布局应用
def radio_selected(self):
radio_btn = self.sender() # rad通过connect方法,而connect方法中绑定了当前函数名,即函数地址,将toggled的信息利用其传输。再利用sender方法获取信息。
if radio_btn.isChecked(): # 如果没有toggle则为空。
self.label.setText(f"You have selected: {radio_btn.text()}")
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())
通过designer设计,然后获取radiobutton状态
关键代码
self.radioButton_first.toggled.connect(self.readio_selected) # 连接功能函数,传递状态
def readio_selected(self):
radio_btn = self.sender() # 获取传递的信号
if radio_btn.isChecked():
self.label_result.setText(f"you have selected: {radio_btn.text()}") # 将结果,通过label_result显示
完整代码
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt6 UI code generator 6.3.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt6 import QtCore, QtGui, QtWidgets
from PyQt6.QtWidgets import QDialog
class Ui_Dialog(QDialog):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
Dialog.setStyleSheet("QDialog{\n"
"\n"
"background-color: yellow\n"
"\n"
"}\n"
"\n"
"QLabel{\n"
"color: green\n"
"\n"
"}")
self.verticalLayout_3 = QtWidgets.QVBoxLayout(Dialog)
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.verticalLayout_2 = QtWidgets.QVBoxLayout()
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.label = QtWidgets.QLabel(Dialog)
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setPointSize(16)
font.setBold(False)
self.label.setFont(font)
self.label.setStyleSheet("")
self.label.setObjectName("label")
self.verticalLayout_2.addWidget(self.label)
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.radioButton_first = QtWidgets.QRadioButton(Dialog)
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
self.radioButton_first.setFont(font)
self.radioButton_first.setObjectName("radioButton_first")
self.radioButton_first.toggled.connect(self.readio_selected) # 连接功能函数,传递状态
self.verticalLayout.addWidget(self.radioButton_first)
self.radioButton_bus = QtWidgets.QRadioButton(Dialog)
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
self.radioButton_bus.setFont(font)
self.radioButton_bus.setObjectName("radioButton_bus")
self.radioButton_bus.toggled.connect(self.readio_selected)
self.verticalLayout.addWidget(self.radioButton_bus)
self.radioButton_eco = QtWidgets.QRadioButton(Dialog)
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
self.radioButton_eco.setFont(font)
self.radioButton_eco.setObjectName("radioButton_eco")
self.radioButton_eco.toggled.connect(self.readio_selected)
self.verticalLayout.addWidget(self.radioButton_eco)
self.verticalLayout_2.addLayout(self.verticalLayout)
self.label_result = QtWidgets.QLabel(Dialog)
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setPointSize(14)
font.setItalic(True)
self.label_result.setFont(font)
self.label_result.setText("")
self.label_result.setObjectName("label_result")
self.verticalLayout_2.addWidget(self.label_result)
self.verticalLayout_3.addLayout(self.verticalLayout_2)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def readio_selected(self):
radio_btn = self.sender() # 获取传递的信号
if radio_btn.isChecked():
self.label_result.setText(f"you have selected: {radio_btn.text()}") # 将结果,通过label_result显示
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.label.setText(_translate("Dialog", "select flight Type"))
self.radioButton_first.setText(_translate("Dialog", "First Class $150"))
self.radioButton_bus.setText(_translate("Dialog", "Busniess Class $120"))
self.radioButton_eco.setText(_translate("Dialog", "Economy Class $100"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec())