Well you gave it a stab that I can respect ;) -- your method though was a bit overly complex and while it could be made to work there is a much simpler way of doing this. Because you have to define each of these objects via code then it is much more efficient to simply put them into the dictionary at the time of creation right after you create them as follows:
Note I removed all the extra inessential things that were not focused on this specific issue but you can easily add them back in if you want the full monty.
Oh and I also changed the name of your variable self.txtNames{} to something more appropriate to what it was and what it was holding self.dctTextHndl{} -- The dct part is short for Dictionary as opposed to a List or lst. TextHndl is short for Textbox Handle -- now it could have been abbreviated however you wanted but whatever you end up using you should do your best to stay consistent as this makes it easier to read your code a month or two down the road. Oh and keeping them short rather than verbose (dictionaryTextboxHandle) makes your code also easier to read as that stuff can get obscenely long.
Note I removed all the extra inessential things that were not focused on this specific issue but you can easily add them back in if you want the full monty.
Oh and I also changed the name of your variable self.txtNames{} to something more appropriate to what it was and what it was holding self.dctTextHndl{} -- The dct part is short for Dictionary as opposed to a List or lst. TextHndl is short for Textbox Handle -- now it could have been abbreviated however you wanted but whatever you end up using you should do your best to stay consistent as this makes it easier to read your code a month or two down the road. Oh and keeping them short rather than verbose (dictionaryTextboxHandle) makes your code also easier to read as that stuff can get obscenely long.
from sys import exit as sysExit from PyQt5.QtCore import QRegExp, Qt from PyQt5.QtGui import QRegExpValidator # Widget Container Objects from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QDockWidget from PyQt5.QtWidgets import QHBoxLayout, QVBoxLayout, QGridLayout, QGroupBox, QLabel # Widget Action Objects from PyQt5.QtWidgets import QLineEdit, QPushButton, QAction, QStyleFactory class CenterPanel(QWidget): def __init__(self, parent): QWidget.__init__(self) self.Parent = parent self.NumRegex = QRegExp("[0-9._]+") self.Validatr = QRegExpValidator(self.NumRegex) self.dctTextHndl = {} # ------- self.lblName = QLabel() self.lblName.setText('Name') self.lblNamTyp = QLabel() self.lblNamTyp.setAlignment(Qt.AlignRight | Qt.AlignTrailing | Qt.AlignVCenter) self.lblNamTyp.setText ('[-]') self.txtName = QLineEdit() self.dctTextHndl[0] = self.txtName # ------- self.lblWeight = QLabel() self.lblWeight.setText ('Weight') self.lblWgtTyp = QLabel() self.lblWgtTyp.setAlignment(Qt.AlignRight | Qt.AlignTrailing | Qt.AlignVCenter) self.lblWgtTyp.setText('[kg]') self.txtWeight = QLineEdit() self.txtWeight.setValidator(self.Validatr) self.dctTextHndl[1] = self.txtWeight # ------- self.lblYear = QLabel() self.lblYear.setText('Year') self.lblYrTyp = QLabel() self.lblYrTyp.setAlignment(Qt.AlignRight | Qt.AlignTrailing | Qt.AlignVCenter) self.lblYrTyp.setText ('[-]') self.txtYear = QLineEdit() self.txtYear.setInputMethodHints(Qt.ImhDigitsOnly) self.dctTextHndl[2] = self.txtYear # ------- self.lblPlace = QLabel() self.lblPlace.setText('Place') self.lblPlcTyp = QLabel() self.lblPlcTyp.setAlignment(Qt.AlignRight | Qt.AlignTrailing | Qt.AlignVCenter) self.lblPlcTyp.setText ('[-]') self.txtPlace = QLineEdit() self.txtPlace.setInputMethodHints(Qt.ImhDigitsOnly) self.dctTextHndl[3] = self.txtPlace # ------- grdTest = QGridLayout() grdTest.addWidget(self.lblName, 0, 0) grdTest.addWidget(QLabel(' '), 0, 1) grdTest.addWidget(self.lblNamTyp, 0, 2) grdTest.addWidget(self.txtName, 0, 3) grdTest.addWidget(self.lblWeight, 1, 0) grdTest.addWidget(QLabel(' '), 1, 1) grdTest.addWidget(self.lblWgtTyp, 1, 2) grdTest.addWidget(self.txtWeight, 1, 3) grdTest.addWidget(self.lblYear, 2, 0) grdTest.addWidget(QLabel(' '), 2, 1) grdTest.addWidget(self.lblYrTyp, 2, 2) grdTest.addWidget(self.txtYear, 2, 3) grdTest.addWidget(self.lblPlace, 3, 0) grdTest.addWidget(QLabel(' '), 3, 1) grdTest.addWidget(self.lblPlcTyp, 3, 2) grdTest.addWidget(self.txtPlace, 3, 3) # ------- gbxTest = QGroupBox() gbxTest.setTitle('Test') gbxTest.setLayout(grdTest) # ------- self.lblHidden = QLabel('') self.HideSet = False self.btnAddNew = QPushButton() self.btnAddNew.setText('Add New') self.btnAddNew.clicked.connect(self.ClearValues) hbxAddNew = QHBoxLayout() hbxAddNew.addWidget(self.lblHidden) hbxAddNew.addStretch(2) hbxAddNew.addWidget(self.btnAddNew) # ------- vbxAll = QVBoxLayout() vbxAll.addWidget(gbxTest) vbxAll.addLayout(hbxAddNew) # ------- gbxDetails = QGroupBox() gbxDetails.setTitle('Details') gbxDetails.setLayout(vbxAll) # ------- hbxFinal = QHBoxLayout() hbxFinal.addWidget(gbxDetails) # ------- self.setLayout(hbxFinal) def ClearValues(self): for idx in range(0, 4): self.dctTextHndl[idx].clear() class MainWindow(QMainWindow): def __init__(self): super(MainWindow, self).__init__() self.setWindowTitle('Main') WinLeft = 150; WinTop = 150; WinWidth = 340; WinHight = 220 self.setGeometry(WinLeft, WinTop, WinWidth, WinHight) self.CenterPane = CenterPanel(self) self.setCentralWidget(self.CenterPane) self.setStyle(QStyleFactory.create('Cleanlooks')) if __name__ == "__main__": MainThred = QApplication([]) MainGui = MainWindow() MainGui.show() sysExit(MainThred.exec_())So as you can see each time a Text Box object is created its handle is put into the object dictionary using a numeric key (any key will work but the numeric one is clearest and easiest when iterating through the dictionary later on) but this is not always the case -- just works best for this one. Still you could have done that function as follows -- which might have been better as its more generic and more pythonic:
def ClearValues(self): for Key in self.dctTextHndl.keys(): self.dctTextHndl[Key].clear()Then in the ClearValues function we iterate through the dictionary using the Idx key (or just the Key in the 2nd one) which gives us the object handle and once we have that handle to the Text object we call that objects .clear() method.