How to pull text from QLineEdits created in for loop

35 views Asked by At

I am creating multiple QLineEdits using PyQt6 in a for loop and I need to pull the text a user enters into each. The problem I am having is that the first QLineEdit does not update its text() or displayText() when text is entered into it. It seems to be pulling its text() and displayText() attributes from the text last entered into the last created QLineEdit. The last QLineEdit works correctly. I initialize the QLineEdits with info from an Excel file. The initial text in each QLineEdit is correct. I need to take the text entered by a user and update the values in the file. Here is the snippet from the code.

for x in range(len(self.mishapsHoursText)):
    self.platformLayout = QHBoxLayout()
    self.mishapsLine = QLineEdit()
    self.hoursLine = QLineEdit()
    self.mishapsLine.setText(str(self.uasParamData.iloc[x]['Mishaps']))
    self.hoursLine.setText(str(self.uasParamData.iloc[x]['Hours']))
    self.platformLayout.addWidget(self.mishapsLine)
    self.platformLayout.addWidget(self.hoursLine)
    self.mainLayout.addLayout(self.platformLayout)
    self.mishapsLine.textChanged.connect(lambda checked, arg1 = x + 2: self.handle_mishaps_hours(arg1, self.mishapsLine.displayText(), "mishaps"))
    self.hoursLine.textChanged.connect(lambda checked, arg1 = x + 2: self.handle_mishaps_hours(arg1, self.hoursLine.displayText(), "hours"))

def handle_mishaps_hours(self, platform, text, choice):
        uasParamData = pd.read_excel('PlatformEcParameters.xlsx') 
        wb = load_workbook('PlatformEcParameters.xlsx')
        ws1 = wb.active
        if choice == "mishaps":
            ws1['H' + str(platform)] = text
        if choice == "hours":
            ws1['I' + str(platform)] = text
        wb.save('PlatformEcParameters.xlsx')

I've tried making the second parameter an argument in the lambda function and that breaks all the QLineEdits. i.e I made the call to the handle_mishaps_hours method look like

self.QLineEdit.textChanged.connect(lambda checked, arg1 = x + 2, arg2 = self.QLineEdit.displayText(): self.handle_mishaps_hours(arg1, arg2, "choice")

I've also tried calling text() instead of displayText() with the same results. I've debugged by printing out the values of the parameters in the call to handle_mishaps_hours(). This is how I know that the last QLineEdit works but the first ones do not.

0

There are 0 answers