Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to design a simple login interface by Python GUI programming

2025-06-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "Python GUI programming how to design a simple login interface". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

Case study: Designing a user login interface

Tools: Python PyQt5

The program runs as follows:

(1) Initial interface

(2) Enter "username" and "password" and click "login"

(4) Click the "Exit" button to exit the interface.

The design procedure is as follows: # -*- coding: utf-8 -*-from PyQt5 import QtCore, QtGui, QtWidgetsfrom PyQt5.QtGui import QPixmap, QIconfrom PyQt5.QtWidgets import QMessageBoxclass 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) #Set text box to password self.lineEdit_2.setValidator(QtGui.QIntValidator(100000,999999)) #Set to enter only 8 digits 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"))) #Set icon for Login button 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) #Set "Administrator radio button selected by default 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"))) #Set icon for Exit button MainWindow.setCentralWidget(self.centralwidget) self.retranslateUi(MainWindow) self.pushButton.clicked.connect(self.login) #Clicked signal binding custom slot function for login button self.pushButton_2.clicked.connect(MainWindow.close) #The clicked signal for the "exit" button binds the close() slot function that comes with the MainWindow window self.radioButton.toggled.connect(self.select) #toggled signal binding custom slot function for radio buttons 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 #Use the information() method to pop up an information prompt box QMessageBox.information(MainWindow, "Login Info, ""Username: " + self.lineEdit.text() + "Password:" + self.lineEdit_2.text(), QMessageBox.Ok) #Custom slot function to determine user login identity def select(self): if self.radioButton.isChecked(): #Determine whether to log in as an administrator QMessageBox.information(MainWindow, "Prompt, ""You selected Admin Login," QMessageBox.Ok) elif self.radioButton_2.isChecked(): #Determine if you are logged in as an ordinary user QMessageBox.information(MainWindow, "Prompt, ""You selected normal user login," QMessageBox.Ok) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "System Login")) self.label.setText(_translate("MainWindow", "username: ")) self.label_2.setText(_translate("MainWindow", "Password: ")) self.pushButton.setText(_translate("MainWindow", "Login")) self.pushButton_2.setText(_translate("MainWindow", "Exit")) self.radioButton.setText(_translate("MainWindow", "Administrator")) self.radioButton_2.setText(_translate("MainWindow", "Normal User"))import sysif __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())"Python GUI programming how to design a simple login interface" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report