在PyQt5中,使用QLineEdit來模擬製作帳號密碼登入UI,首先使用Qt designer來拉出元件
如以下畫面所示,拉出Label、QLineEdit、QCheckBox與QpushButton,同時在designer中設定QCheckBox與QPushButton的信號-槽
- QCheckBox搭配show_pw訊號,用來控制當勾選此checkBox時,就顯示密碼
- QPushButton搭配login_test訊號,當點擊時就開始進行帳號密碼的登入測試,這邊只印出log,沒有實際的進行實質的登入
- 密碼可見與不可見,使用QLineEdit原生的setEchoMode與QLineEdit.Password進行設定
- PasswordEchoOnEdit效果不好,必須等當前QLineEdit失去焦點才能將密碼不可見,在簡單的UI元件下,不好控制,除非再額外新增相關焦點控制
範例程式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import os import sys from PyQt5 import QtCore, QtWidgets, QtGui from login_ui import Ui_Form class MainWindow(QtWidgets.QMainWindow, Ui_Form): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.setupUi(self) def login_test(self): print("login_test") print(self.ac_lineEdit.text()) print(self.pw_lineEdit.text()) print(self.checkBox.isChecked()) def show_pw(self): if self.checkBox.isChecked(): self.pw_lineEdit.setEchoMode(QtWidgets.QLineEdit.Normal) else: self.pw_lineEdit.setEchoMode(QtWidgets.QLineEdit.Password) def closeEvent(self, event): QtWidgets.QApplication.closeAllWindows() if __name__== "__main__": app = QtWidgets.QApplication(sys.argv) win = MainWindow() win.show() sys.exit(app.exec_()) |
UI檔案
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(470, 200) Form.setMinimumSize(QtCore.QSize(470, 200)) Form.setMaximumSize(QtCore.QSize(470, 200)) font = QtGui.QFont() font.setFamily("Bahnschrift Light SemiCondensed") Form.setFont(font) Form.setWindowTitle("") self.ac_lineEdit = QtWidgets.QLineEdit(Form) self.ac_lineEdit.setGeometry(QtCore.QRect(140, 30, 221, 31)) font = QtGui.QFont() font.setFamily("Courier New") font.setPointSize(12) self.ac_lineEdit.setFont(font) self.ac_lineEdit.setObjectName("ac_lineEdit") self.pw_lineEdit = QtWidgets.QLineEdit(Form) self.pw_lineEdit.setGeometry(QtCore.QRect(140, 79, 221, 31)) font = QtGui.QFont() font.setFamily("Courier New") font.setPointSize(12) self.pw_lineEdit.setFont(font) self.pw_lineEdit.setEchoMode(QtWidgets.QLineEdit.Password) # self.pw_lineEdit.setEchoMode(QtWidgets.QLineEdit.PasswordEchoOnEdit) self.pw_lineEdit.setObjectName("pw_lineEdit") self.label = QtWidgets.QLabel(Form) self.label.setGeometry(QtCore.QRect(30, 30, 91, 31)) font = QtGui.QFont() font.setFamily("Courier New") font.setPointSize(12) self.label.setFont(font) self.label.setObjectName("label") self.label_2 = QtWidgets.QLabel(Form) self.label_2.setGeometry(QtCore.QRect(30, 80, 91, 31)) font = QtGui.QFont() font.setFamily("Courier New") font.setPointSize(12) self.label_2.setFont(font) self.label_2.setObjectName("label_2") self.pushButton = QtWidgets.QPushButton(Form) self.pushButton.setGeometry(QtCore.QRect(30, 140, 411, 31)) font = QtGui.QFont() font.setFamily("Courier New") font.setPointSize(12) self.pushButton.setFont(font) self.pushButton.setObjectName("pushButton") self.checkBox = QtWidgets.QCheckBox(Form) self.checkBox.setGeometry(QtCore.QRect(380, 70, 81, 41)) font = QtGui.QFont() font.setFamily("Courier New") font.setPointSize(12) self.checkBox.setFont(font) self.checkBox.setObjectName("checkBox") self.retranslateUi(Form) self.pushButton.clicked.connect(Form.login_test) # type: ignore self.checkBox.clicked['bool'].connect(Form.show_pw) # type: ignore QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Login")) self.label.setText(_translate("Form", "account:")) self.label_2.setText(_translate("Form", "password:")) self.pushButton.setText(_translate("Form", "Login")) self.checkBox.setText(_translate("Form", "show")) |
最終UI畫面效果如下,包含密碼可見與不可見
勾選show checkbox則密碼可見
預設密碼不可見