Display data from list in label Python

前端 未结 1 1453
挽巷
挽巷 2021-01-29 02:52

I have a list of data and the size of list is not fix. I want to display each item of this list in a label(Textview).

        self.valueT.append(value) 
                


        
1条回答
  •  借酒劲吻你
    2021-01-29 03:41

    A for-loop runs so fast that our slow brains can't pick it up, so you can't see the text. The idea of executing it every T seconds helps that this is not a problem but you do not have to use a for-loop but write it using a QTimer plus an iterator, that is, it is the same logic of iterating but using the timer events:

    import sys
    
    from PyQt5 import QtCore, QtGui, QtWidgets
    
    
    class MainWindow(QtWidgets.QMainWindow):
        def __init__(self, parent=None):
            super(MainWindow, self).__init__(parent)
    
            self.label = QtWidgets.QLabel(alignment=QtCore.Qt.AlignCenter)
            self.setCentralWidget(self.label)
    
            self.resize(640, 480)
    
            listT = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
    
            self.listT_iterator = iter(listT)
    
            self.timer = QtCore.QTimer(timeout=self.on_timeout, interval=1000)
            self.timer.start()
    
            self.on_timeout()
    
        @QtCore.pyqtSlot()
        def on_timeout(self):
            try:
                value = next(self.listT_iterator)
                self.label.setText(value)
            except StopIteration:
                self.timer.stop()
    
    
    if __name__ == "__main__":
        app = QtWidgets.QApplication(sys.argv)
        w = MainWindow()
        w.show()
        sys.exit(app.exec_())
    

    0 讨论(0)
提交回复
热议问题