Search code examples
python-3.xpyqtpyqt4qlineedit

QLineEdit.setText only works once in function


I'm currently trying to program for my bachelor thesis. The main part works, so now I want to implement a user interface. I watched some tutorials and worked via trial and error, and my user interface also works. So far so good. But yesterday I changed a small thing and that doesn't do what i want. I have a button saying "start program", and a line-edit where i want to display the current status. My code is:

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_())

Everything works as I want except from lineedit.setText('Running'). What I want is that "Running" is displayed while otherfile.therealfunction is working. I guess I have to update the line-edit somehow? But until now I didn't figure out how I can do that. I also set the line-edit readonly because I don't want the user to be able to change it, so maybe that's a problem? I thought readonly would only affect what the user can do.

I am using Python3 and PyQt4 with Qt Designer.


Solution

  • Calling otherfile.therealfunction() will block all ui updates until the function completes. You can try forcing immediate ui updates like this:

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