RforMaser
RforMaser

Reputation: 29

how can i display data like a real time update? (pyqt)

i got a problem that i have a method that continuously generate number and another method will call this method and display it on the Qtextedit when the button is clicked but the GUI seems no response

there are my example code

class Ui_MainWindow(object):
 def setupUi(self, MainWindow):
  MainWindow.setObjectName(_fromUtf8("MainWindow"))
  MainWindow.resize(176, 156)
  self.centralWidget = QtGui.QWidget(MainWindow)
  self.centralWidget.setObjectName(_fromUtf8("centralWidget"))
  self.gridLayout = QtGui.QGridLayout(self.centralWidget)
  self.pushButton = QtGui.QPushButton(self.centralWidget)
  self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)

  self.text = QTextEdit()
  self.gridLayout.addWidget(self.text, 1, 0, 1, 1)

  self.pushButton.clicked.connect( self.Out)


  out = 0
  def number(self):
    i = 0
    while True:
        i += 1
        time.sleep(0.5)
        out = str(i)

  def Out(self):
    time.sleep(0.5)
    self.text.append(self.number())
    QtGui.qApp.processEvents()

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

anyone can give me suggestion?

Upvotes: 1

Views: 4969

Answers (1)

Joël
Joël

Reputation: 2832

Your while True: is a blocking call: once you enter this function, you don't go out. Therefore, your script hangs here.

One solution would be to execute that function in a separate thread (and it would therefore be non-blocking); another one would be to use a timer and a signal, as explained in the answer to the following question.

Upvotes: 2

Related Questions