需求:在pyqt5中打开一个界面,在lineEdit中输入账号密码,下次打开软件的时候出现上次输入的账号密码
首先使用设计师绘制界面
在界面中输入账号密码,点击按钮
下次打开界面时会出现上次输入的账号密码
代码如下(不能直接运行)?
import os
from PyQt5 import QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from curveChooserUI import Ui_MainWindow
class CurveChooseGUI(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(CurveChooseGUI, self).__init__(parent)
self.setupUi(self)
self.init_login_info()
self.pushButton.clicked.connect(self.on_pushButton_clicked)##点击按钮进行保存
def on_pushButton_clicked(self):
self.save_login_info()
##保存lineEdit中内容
def save_login_info(self):
settings = QSettings("config.ini", QSettings.IniFormat)
settings.setValue("account",self.lineEdit.text())
settings.setValue("password", self.lineEdit_2.text())
##显示上次保存的值
def init_login_info(self):
settings = QSettings("config.ini", QSettings.IniFormat)
the_account = settings.value("account")
the_password = settings.value("password")
self.lineEdit.setText(the_account)
self.lineEdit_2.setText(the_password)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
loadedFontID = QFontDatabase.addApplicationFont(
os.path.join(os.path.dirname(os.path.abspath(__file__)), "Resource", "Font", "wqy-microhei.ttc"))
# font_dirs = [os.path.join("Resource", "Font", )]
# font_files = font_manager.findSystemFonts(fontpaths=font_dirs)
# font_list = font_manager.createFontList(font_files)
# font_manager.fontManager.ttflist.extend(font_list)
gui = CurveChooseGUI()
app.setStyleSheet(
'''
QWidget
{
font: 12pt "文泉驿微米黑";
}
QComboBox QAbstractItemView
{
min-height: 30px;
}
''')
gui.show()
sys.exit(app.exec_())
|