I am having an issue updating a table_widget in PYQT5

21 Views Asked by At

using the following code the file is being created and then read by the code to update the table_widget. The table is not being updated in the gui. Print statements show that the data is being read but the rows are left blank.

def rebuild_setup(self):
    try:
        print("Current working directory:", os.getcwd())

        # Get the file prefixes
        file_prefixes = [filename for filename in os.listdir() if filename.startswith('in_')]

        print("File prefixes:", file_prefixes)

        # Write the file pairs to the file
        with open('file_pairs.txt', 'w') as f:
            try:
                for filename in file_prefixes:
                    f.write(filename + ',out_' + filename[3:] + '\n')

                print (os.getcwd())
                print(os.path.abspath('file_pairs.txt'))
                print("File Rebuilt!")
            except Exception as e:
                print("An error occurred while writing to the file:", str(e))

        # Check if the file exists
        if os.path.exists('file_pairs.txt'):
            print("'file_pairs.txt' exists.")
        else:
            print("'file_pairs.txt' does not exist.")

        # Read the data from the file
        with open('file_pairs.txt', 'r') as f:
            self.file_pairs = [tuple(line.strip().split(',')) for line in f]

        print("File pairs:", self.file_pairs)

        # Update the table
        self.table_widget.setRowCount(len(self.file_pairs))

        for i, (in_file, out_file) in enumerate(self.file_pairs):
            print("Adding row", i, ":", in_file, ",", out_file)

            in_item = QTableWidgetItem()
            in_item.setText(in_file)
            print("In item text:", in_item.text())
            self.table_widget.setItem(i, 0, in_item)

            out_item = QTableWidgetItem()
            out_item.setText(out_file)
            print("Out item text:", out_item.text())
            self.table_widget.setItem(i, 1, out_item)
        self.table_widget.Update 
        self.table_widget.viewport().update()
        print("Table Rebuilt!")

    except Exception as e:
        print("An error occurred:", str(e))
0

There are 0 best solutions below