使用自带的函数就可以实现: lineEdit.setEchoMode(QLineEdit.Password)
import struct from PyQt5.QtWidgets import (QMessageBox, QLineEdit) from PyQt5.QtGui import QPixmap {...} class LoginWindow(QtWidgets.QWidget): {...} def __init__(self): super(LoginWindow, self).__init__() # 加载ui文件 loadUi('login.ui', self) # 设置背景图片 background = QPixmap("img/login_background.jpeg") background = background.scaled(self.background_label.width(),self.background_label.width()) self.background_label.setPixmap(background) # 设置输入密码框 self.passwd_lineEdit.setEchoMode(QLineEdit.Password) {.......} def mainpage(): app = QtWidgets.QApplication(sys.argv) login_window = LoginWindow() login_window.show() sys.exit(app.exec_())