[Python] pyqt5 TableWidget won't display items until function is complete

Discussão em 'Python' iniciado por Stack, Maio 12, 2025.

  1. Stack

    Stack Membro Participativo

    Alright, I am a new python programmer and especially new to GUI Programming (so Please be gently on my programming style)

    I am writing a simple program that will read a series of 10 sensors, all of which come in on an individual COM Port, then display the data in a table using pyqt5.

    so far, it is doing all of that, however, instead of displaying the data immediately when I write the "self.DataTable.setItem()" function, the data doesn't display until my connect function is complete.

    def FunctionConnect(self):
    self.ScanComPB.clicked.connect(self.ScanComPBClicked)
    self.CloseComPB.clicked.connect(self.CloseComPBClicked)


    def ScanComPBClicked(self):
    print("Scan Com Ports Clicked")

    Port = serial.tools.list_ports.comports()


    for OnePort in Port:
    self.PortList.append(OnePort.device)

    PortListLen = len(self.PortList)
    PortListLenSplit = math.ceil(PortListLen/2)

    List1 = self.PortList[:portListLenSplit]
    List2 = self.PortList[PortListLenSplit:]

    self.COMList1.addItems(List1)
    self.COMList2.addItems(List2)

    self.ComRow = 2


    for Ports in self.PortList:

    #Reset Dictionary Data
    self.SensorDict["ComPort"] = ""
    self.SensorDict["EEPROM"] = []

    print(Ports)
    self.SensorDict["ComPort"] = Ports
    self.DataTable.setItem(self.ComRow,0,QTableWidgetItem(Ports))

    try:
    self.TestPort = serial.Serial(Ports, self.BaudRate, timeout = self.TimeOut)

    except serial.SerialException:
    self.DataTable.setItem(self.ComRow,0,QTableWidgetItem("Serial Exception"))
    self.TestPort.close()

    #Read Sensor EEPROM, and loads it into self.SensorDict["EEPROM"]
    self.ReadSensorEEPROM()
    print ("Read EEPROM")

    if self.SensorDict["EEPROM"]:
    #Gets sensor type and loads it into self.SensorDict["Sensor Type"]
    self.GetSensorType()

    print ("Read Sensor Type")
    self.DataTable.setItem(self.ComRow,1,QTableWidgetItem(self.SensorDict["Sensor Type"]))

    else:
    self.DataTable.setItem(self.ComRow,1,QTableWidgetItem("Invalid"))
    self.SensorDict["Pass/Fail"] = False

    self.TestPort.close()
    self.ComRow += 1


    (Hopefully that's enough of a snippet to understand the problem) programming in spyder Python 3.1.2 pyqty, on a windows 11 PC.

    The "print" Statements that I have right before the setItem function calls all print to the terminal at the correct time.

    and the data is all there, in the correct spot once it does display, but I would like to see them all display as each value is collected, not after everything is done.

    Thanks for the help, I don't even really know where to start with this one

    mostly I have just tried to understand the problem. I couldn't find any other similar questions, and none of the documentation or tutorials say anything about a separate command to display the table. they all seem to just appear right away.

    my only guess is something along the lines of because it's in a connect function from a push button it has to wait to complete the push button function before the table widget can do anything. in which case I'm not quite sure how to properly order things.

    Continue reading...

Compartilhe esta Página