简体   繁体   English

在pyqt中显示运行时输出

[英]Displaying run-time output in pyqt

I have python code for simple displaying the output in the mainwindow after clicking the pushbutton. 我有python代码,可在单击按钮后在主窗口中简单地显示输出。 Here is my code: 这是我的代码:

from PyQt4 import QtCore, QtGui
import time

try:
  _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
  _fromUtf8 = lambda s: s

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.gridLayout.setObjectName(_fromUtf8("gridLayout"))
    self.pushButton = QtGui.QPushButton(self.centralWidget)
    self.pushButton.setObjectName(_fromUtf8("pushButton"))
    self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
    self.label = QtGui.QLabel(self.centralWidget)
    self.label.setObjectName(_fromUtf8("label"))
    self.gridLayout.addWidget(self.label, 1, 0, 1, 1)
    MainWindow.setCentralWidget(self.centralWidget)
    self.menuBar = QtGui.QMenuBar(MainWindow)
    self.menuBar.setGeometry(QtCore.QRect(0, 0, 176, 25))
    self.menuBar.setObjectName(_fromUtf8("menuBar"))
    MainWindow.setMenuBar(self.menuBar)
    self.mainToolBar = QtGui.QToolBar(MainWindow)
    self.mainToolBar.setObjectName(_fromUtf8("mainToolBar"))
    MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
    self.statusBar = QtGui.QStatusBar(MainWindow)
    self.statusBar.setObjectName(_fromUtf8("statusBar"))
    MainWindow.setStatusBar(self.statusBar)

    self.retranslateUi(MainWindow)
    QtCore.QMetaObject.connectSlotsByName(MainWindow)
    self.pushButton.clicked.connect( self.Out)

  def retranslateUi(self, MainWindow):
    MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
    self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "Start", None, QtGui.QApplication.UnicodeUTF8))
    self.label.setText(QtGui.QApplication.translate("MainWindow", "0.0", None, QtGui.QApplication.UnicodeUTF8))

  def Out(self):
    for i in range(10):
        time.sleep(0.5)
        out = str(i)
        self.label.setText(out)
        if i == 10:
            break
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_())

But i want continuous output in label and it is giving after the executing of program. 但是我想在标签中连续输出,并且在程序执行后给出。 Please suggest correction in code or any trick. 请提出更正代码或任何技巧的建议。 Sorry for weak English. 对不起,英语不好。

For your example code, you can use processEvents to keep the gui updated: 对于示例代码,可以使用processEvents来保持gui更新:

def Out(self):
    for i in range(10):
        time.sleep(0.5)
        out = str(i)
        self.label.setText(out)
        QtGui.qApp.processEvents()
        if i == 10:
            break

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM