'python add another same textedit and button

I want to make that, when I press the plus, another same form is made. The second form appeared but the first form was lost I need help when I keep the first one and add the others below

from zlib import DEF_BUF_SIZE
from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(459, 288)
       
        self.y=15
        self.x=20
        self.i=0
        self.rep(self.y, self.x)
        
        self.retranslateUi(MainWindow)
        #QtCore.QMetaObject.connectSlotsByName(MainWindow)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))


    def rep(self,x,y):
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("../../../Downloads/Hopstarter-Button-Button-Add.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        MainWindow.setWindowIcon(icon)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget" + str(self.i))
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(30, x, 200, 30))
        font = QtGui.QFont()
        font.setPointSize(14)
        font.setWeight(75)
        self.lineEdit.setFont(font)
        self.lineEdit.setObjectName("lineEdit")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(250, y, 75, 23))
        self.pushButton.setText("")
        self.pushButton.setIcon(icon)
        self.pushButton.setIconSize(QtCore.Size(21, 21))
        self.pushButton.setDefault(True)

        self.pushButton.setObjectName("pushButton")        
        self.pushButton.clicked.connect(self.onOne)
           
        MainWindow.setCentralWidget(self.centralwidget) 
        pass
    
    def onOne(self):
        self.y +=30
        self.x +=30
        self.i += 1
  
        self.rep(self.y, self.x)
        pass

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())


Solution 1:[1]

I tried the first suggestion and it didn't work. I listened to another suggestion. He modified something of his own, and it works

from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import (QWidget, QPushButton, QHBoxLayout, QVBoxLayout, QApplication)

class Ui_MainWindow(QWidget):

def __init__(self):
    super().__init__()
    
    self.vbox = QVBoxLayout()
    self.setupUi()
            
def setupUi(self):  
    self.hbox = QHBoxLayout()
    
    self.lineEdit = self.lab()
    self.hbox.addWidget(self.lineEdit) 
    
    self.hbox.addStretch()
    
    self.pushButton = self.butt()
    self.hbox.addWidget(self.pushButton)
    
    self.vbox.addLayout(self.hbox)
    
    self.setLayout(self.vbox)

    self.pushButton.clicked.connect(self.klik)
    
def klik(self):
    s = self.lineEdit.text()
    if (len(s.strip())!=0):
        self.pushButton.setEnabled(False)
        self.setupUi()
    pass    

def f(self):
    font = QtGui.QFont()
    font.setPointSize(14)
    font.setBold(True)
    font.setWeight(75)
    return font
    
def i(self):
    icon = QtGui.QIcon()
    icon.addPixmap(QtGui.QPixmap("../../../Users/Downloads/Hopstarter-Button-Button-Add.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
    return icon
    
def butt(self):
    pushButton = QtWidgets.QPushButton()
    ff = self.f()
    pushButton.setFont(ff)
    pushButton.setLayoutDirection(QtCore.Qt.LeftToRight)
    pushButton.setText("")
    
    ii = self.i() 
    pushButton.setIcon(ii)
    pushButton.setIconSize(QtCore.QSize(21, 21))
    pushButton.setAutoDefault(True)
    pushButton.setDefault(True)
    pushButton.setFlat(True)
    pushButton.setObjectName("pushButton")
    return pushButton
        
def lab(self):
    lineEdit = QtWidgets.QLineEdit()
    ff = self.f()
    lineEdit.setFont(ff)
    
    lineEdit.setObjectName("lineEdit")        
    return lineEdit
    

if name == "main": import sys app = QtWidgets.QApplication(sys.argv) ui = Ui_MainWindow() ui.show() sys.exit(app.exec_())

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1