Ну Роман написал, я нарисовал. Изучайте.
main.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(627, 346)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.lineEdit_param1 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_param1.setObjectName("lineEdit_param1")
self.gridLayout.addWidget(self.lineEdit_param1, 0, 2, 1, 1)
self.lineEdit_param2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_param2.setObjectName("lineEdit_param2")
self.gridLayout.addWidget(self.lineEdit_param2, 1, 2, 1, 1)
self.pushButton_save = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_save.setObjectName("pushButton_save")
self.gridLayout.addWidget(self.pushButton_save, 2, 0, 1, 1)
self.pushButton_load = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_load.setObjectName("pushButton_load")
self.gridLayout.addWidget(self.pushButton_load, 2, 1, 1, 1)
self.label_param1 = QtWidgets.QLabel(self.centralwidget)
self.label_param1.setObjectName("label_param1")
self.gridLayout.addWidget(self.label_param1, 0, 1, 1, 1)
self.label_param2 = QtWidgets.QLabel(self.centralwidget)
self.label_param2.setObjectName("label_param2")
self.gridLayout.addWidget(self.label_param2, 1, 1, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 627, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.lineEdit_param1.setPlaceholderText(_translate("MainWindow", "Enter Param 1"))
self.lineEdit_param2.setPlaceholderText(_translate("MainWindow", "Enter Param 2"))
self.pushButton_save.setText(_translate("MainWindow", "Save"))
self.pushButton_load.setText(_translate("MainWindow", "Load"))
self.label_param1.setText(_translate("MainWindow", "Param1"))
self.label_param2.setText(_translate("MainWindow", "Param2"))
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pushButton_save.clicked.connect(self.save_params)
self.pushButton_load.clicked.connect(self.load_params)
def save_params(self):
with open('params.txt', 'w') as f:
f.write(f'{self.lineEdit_param1.text()}\n')
f.write(f'{self.lineEdit_param2.text()}\n')
msg = QtWidgets.QMessageBox.information(self, 'Save', 'Параметры сохранены.')
self.lineEdit_param1.clear()
self.lineEdit_param2.clear()
def load_params(self):
file, _ = QtWidgets.QFileDialog.getOpenFileName(self, 'Open File', './', 'Files (*.txt)')
if file:
with open(file, 'r') as f:
self.lineEdit_param1.setText(f.readline())
self.lineEdit_param2.setText(f.readline())
msg = QtWidgets.QMessageBox.information(self, 'Load', 'Параметры загружены.')
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
app.setFont(QtGui.QFont("Times", 15, QtGui.QFont.Bold))
w = MainWindow()
w.show()
sys.exit(app.exec_())
param_1_2.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>627</width>
<height>346</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="2">
<widget class="QLineEdit" name="lineEdit_param1">
<property name="placeholderText">
<string>Enter Param 1</string>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QLineEdit" name="lineEdit_param2">
<property name="placeholderText">
<string>Enter Param 2</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QPushButton" name="pushButton_save">
<property name="text">
<string>Save</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QPushButton" name="pushButton_load">
<property name="text">
<string>Load</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLabel" name="label_param1">
<property name="text">
<string>Param1</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel" name="label_param2">
<property name="text">
<string>Param2</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>627</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>