2016-07-22 30 views
0

我目前正在嘗試編寫我的學士論文。主要部分起作用,所以現在我想實現一個用戶界面。我看了一些教程,並通過試驗和錯誤工作,我的用戶界面也可以工作。到現在爲止還挺好。但是昨天我改變了一件小事,那不符合我的意願。我有一個按鈕說「開始程序」,並在我想要顯示當前狀態的行編輯。我的代碼是:QLineEdit.setText只能在函數中使用一次

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')除外。我想要的是顯示「正在運行」,而otherfile.therealfunction正在工作。我想我不得不更新行編輯?但直到現在我還沒有弄清楚我該怎麼做。我還設置了只讀行,因爲我不希望用戶能夠更改它,所以也許這是一個問題?我認爲readonly只會影響用戶可以做的事情。

我正在使用Python3和PyQt4與Qt設計器。

回答

0

調用otherfile.therealfunction()將阻止所有ui更新,直到函數完成。你可以嘗試迫使立即像這樣的UI更新:

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

謝謝。那完全按照我想要的那樣工作 –