QLineEdit.setText 在函数中只工作一次

QLineEdit.setText only works once in function

我目前正在为我的学士论文编写程序。主要部分有效,所以现在我想实现一个用户界面。我看了一些教程并通过反复试验工作,我的用户界面也能正常工作。到目前为止,一切都很好。但是昨天我改变了一件小事,但这并没有达到我想要的效果。我有一个按钮说 "start program",还有一个行编辑,我想在其中显示当前状态。我的代码是:

import sys
from PyQt4 import QtGui
from theguifile import Ui_MainWindow
import otherfile

class Main(QtGui.QMainWindow):

    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.ui=Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.mybutton.clicked.connect(self.runprogram)

   def runprogram(self):
        self.ui.mylineedit.setText('Running') # doesnt work
        try:
            otherfile.therealfunction() # works
        except ErrorIwanttocatch:
            self.ui.mylineedit.setText('thisErrorhappened') # works
        else:
            self.ui.mylineedit.setText('Success') # works

app = QtGui.QApplication(sys.argv)
window = Main()
sys.exit(app.exec_())

除了 lineedit.setText('Running') 之外,一切都如我所愿。我想要的是 "Running" 在 otherfile.therealfunction 工作时显示。我想我必须以某种方式更新行编辑?但直到现在我还没有弄清楚我该怎么做。我还将行编辑设置为只读,因为我不希望用户能够更改它,所以这可能是个问题?我以为 readonly 只会影响用户可以做什么。

我在 Qt Designer 中使用 Python3 和 PyQt4。

调用 otherfile.therealfunction() 将阻止所有 ui 更新,直到函数完成。您可以尝试像这样强制立即 ui 更新:

   def runprogram(self):
        self.ui.mylineedit.setText('Running')
        QtGui.qApp.processEvents()