Python-pyqt5之RadioButton:单选按钮

RadioButton是单选按钮控件,它为用户提供两个或多个互斥选项组成的选项集,当用户选中某单选按钮时,同一组中的其他单选按钮不能同时选定。

任务目标:

输入用户名密码,选择登录角色,按回车键或点击登录可显示登录信息,点击退出,可退出程序

设计:

Python-pyqt5之RadioButton:单选按钮_第1张图片

完整代码:

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMessageBox

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(459, 308)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(200, 50, 113, 21))
        self.lineEdit.setObjectName("lineEdit")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(130, 50, 61, 21))
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(130, 90, 81, 21))
        self.label_2.setObjectName("label_2")
        self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_2.setGeometry(QtCore.QRect(200, 90, 113, 21))
        self.lineEdit_2.setObjectName("lineEdit_2")

        self.lineEdit_2.setEchoMode(QtWidgets.QLineEdit.Password)
        self.lineEdit_2.setValidator(QtGui.QIntValidator(10000000,99999999))

        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(130, 150, 81, 28))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(230, 150, 81, 28))
        self.pushButton_2.setObjectName("pushButton_2")
        self.radioButton = QtWidgets.QRadioButton(self.centralwidget)
        self.radioButton.setGeometry(QtCore.QRect(140, 120, 81, 21))
        self.radioButton.setObjectName("radioButton")
        self.radioButton.setChecked(True) #设置管理员单选按钮默认选中
        self.radioButton_2 = QtWidgets.QRadioButton(self.centralwidget)
        self.radioButton_2.setGeometry(QtCore.QRect(220, 120, 91, 21))
        self.radioButton_2.setObjectName("radioButton_2")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 459, 29))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        self.pushButton.clicked.connect(self.login)
        self.pushButton_2.clicked.connect(MainWindow.close)
        self.radioButton.toggled.connect(self.select)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
        self.set_shortcut(MainWindow)

    # 设置快捷键
    def set_shortcut(self, MainWindow):
        login_shortcut = QtWidgets.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Return), MainWindow)
        login_shortcut.activated.connect(self.login)


    def login(self):
        # 使用information()方法弹出信息提示框
        QMessageBox.information(MainWindow, "登录信息", "用户名:"+self.lineEdit.text()+"  密码:"+self.lineEdit_2.text(), QMessageBox.Ok)

    # 自定义槽函数,用来判断用户登录身份
    def select(self):
        if self.radioButton.isChecked(): # 判断是否为管理员登录
            QMessageBox.information(MainWindow, "提示","您选择的是 管理员  登录", QMessageBox.Ok)
        elif self.radioButton_2.isChecked(): # 判断是否为普通用户登录
            QMessageBox.information(MainWindow, "提示", "您选择的是 普通用户  登录", QMessageBox.Ok)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label.setText(_translate("MainWindow", "用户名:"))
        self.label_2.setText(_translate("MainWindow", "密  码:"))
        self.pushButton.setText(_translate("MainWindow", "登录"))
        self.pushButton_2.setText(_translate("MainWindow", "退出"))
        self.radioButton.setText(_translate("MainWindow", "管理员"))
        self.radioButton_2.setText(_translate("MainWindow", "普通用户"))


import sys
if __name__ == '__main__':
    app=QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

效果:

Python-pyqt5之RadioButton:单选按钮_第2张图片

Python-pyqt5之RadioButton:单选按钮_第3张图片

你可能感兴趣的:(Python学习,python,qt,开发语言,qt5)