您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“Python GUI編程如何設(shè)計(jì)一個(gè)簡(jiǎn)單的登錄界面”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
案例:設(shè)計(jì)一個(gè)用戶登錄界面
工具:python PyQt5
(1)初始界面
(2)輸入“用戶名”和“密碼”,點(diǎn)擊“登錄”
(4)點(diǎn)擊“退出”按鈕后界面退出
# -*- coding: utf-8 -*- from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtGui import QPixmap, QIcon from PyQt5.QtWidgets import QMessageBox class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(300, 180) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.label = QtWidgets.QLabel(self.centralwidget) self.label.setGeometry(QtCore.QRect(60, 30, 51, 16)) self.label.setObjectName("label") self.label_2 = QtWidgets.QLabel(self.centralwidget) self.label_2.setGeometry(QtCore.QRect(60, 60, 51, 16)) self.label_2.setObjectName("label_2") self.lineEdit = QtWidgets.QLineEdit(self.centralwidget) self.lineEdit.setGeometry(QtCore.QRect(120, 30, 113, 20)) self.lineEdit.setObjectName("lineEdit") self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget) self.lineEdit_2.setGeometry(QtCore.QRect(120, 60, 113, 20)) self.lineEdit_2.setObjectName("lineEdit_2") self.lineEdit_2.setEchoMode(QtWidgets.QLineEdit.Password) # 設(shè)置文本框?yàn)槊艽a self.lineEdit_2.setValidator(QtGui.QIntValidator(10000000,99999999)) # 設(shè)置只能輸入8位數(shù)字 self.pushButton = QtWidgets.QPushButton(self.centralwidget) self.pushButton.setGeometry(QtCore.QRect(70, 120, 61, 27)) self.pushButton.setObjectName("pushButton") self.pushButton.setIcon(QIcon(QPixmap("login.ico"))) # 為“登錄”按鈕設(shè)置圖標(biāo) self.label = QtWidgets.QLabel(self.centralwidget) self.label.setGeometry(QtCore.QRect(60, 30, 51, 16)) self.label.setObjectName("label") self.label_2 = QtWidgets.QLabel(self.centralwidget) self.label_2.setGeometry(QtCore.QRect(60, 60, 51, 16)) self.label_2.setObjectName("label_2") self.radioButton = QtWidgets.QRadioButton(self.centralwidget) self.radioButton.setGeometry(QtCore.QRect(70, 90, 71, 18)) self.radioButton.setObjectName("radioButton") self.radioButton.setChecked(True) # 設(shè)置“管理員單選按鈕默認(rèn)選中 self.radioButton_2 = QtWidgets.QRadioButton(self.centralwidget) self.radioButton_2.setGeometry(QtCore.QRect(150, 90, 81, 18)) self.radioButton_2.setObjectName("radioButton_2") self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_2.setGeometry(QtCore.QRect(160, 120, 61, 27)) self.pushButton_2.setObjectName("pushButton_2") self.pushButton_2.setIcon(QIcon(QPixmap("exit.ico"))) # 為“退出”按鈕設(shè)置圖標(biāo) MainWindow.setCentralWidget(self.centralwidget) self.retranslateUi(MainWindow) self.pushButton.clicked.connect(self.login) # 為“登錄”按鈕的clicked信號(hào)綁定自定義槽函數(shù) self.pushButton_2.clicked.connect(MainWindow.close) # 為“退出”按鈕的clicked信號(hào)綁定MainWindow窗口自帶的close()槽函數(shù) self.radioButton.toggled.connect(self.select) # 為單選按鈕的toggled信號(hào)綁定自定義槽函數(shù) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 317, 26)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) QtCore.QMetaObject.connectSlotsByName(MainWindow) def login(self): from PyQt5.QtWidgets import QMessageBox # 使用information()方法彈出信息提示框 QMessageBox.information(MainWindow, "登錄信息", "用戶名: " + self.lineEdit.text() + " 密碼:" + self.lineEdit_2.text(), QMessageBox.Ok) # 自定義槽函數(shù),用來判斷用戶登錄身份 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", "系統(tǒng)登錄")) 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 GUI編程如何設(shè)計(jì)一個(gè)簡(jiǎn)單的登錄界面”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。