I have had a go but keep in mind that I don't use pyside/pyqt so its just thrown together from looking at random tutorials.
The frame does not resize based on the widgets, I didn't figure that out yet.
Note: I had PyQt5 installed with anaconda so I just changed all the imports, hopefully, it works in PySide2 too.
import sys
from PySide2 import QtCore, QtWidgets
from PySide2.QtWidgets import QMainWindow, QWidget, QLabel, QLineEdit, QFormLayout
from PySide2.QtWidgets import QPushButton, QVBoxLayout, QHBoxLayout
from PySide2.QtCore import QSize
class MainWindow(QWidget):
def __init__(self):
QWidget.__init__(self)
self.setMinimumSize(QSize(320, 220))
self.setWindowTitle(
"PyQt Line Edit example (textfield) - pythonprogramminglanguage.com")
self.controls = []
button1 = QPushButton('add field', self)
button1.clicked.connect(self.add_field)
button2 = QPushButton('remove field', self)
button2.clicked.connect(self.remove_field)
button3 = QPushButton('OK', self)
button3.clicked.connect(self.clickMethod)
vbox = QVBoxLayout()
formlayout = QFormLayout()
vbox.addLayout(formlayout)
hbox = QHBoxLayout()
hbox.addWidget(button1)
hbox.addWidget(button2)
vbox.addLayout(hbox)
vbox.addWidget(button3)
self.setLayout(vbox)
self.formlayout = formlayout
self.add_field()
def add_field(self):
edit = QLineEdit(self)
self.controls.append(edit)
self.formlayout.addRow('Name', edit)
def remove_field(self):
if len(self.controls) > 1:
edit = self.controls.pop()
self.formlayout.removeRow(edit)
def clickMethod(self):
names = ', '.join(edit.text() for edit in self.controls)
print(f'Names: {names}')
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())