【问题标题】:QLineEdit.setText only works once in functionQLineEdit.setText 在函数中只工作一次
【发布时间】:2016-07-22 11:33:43
【问题描述】:

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

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 工作时显示“正在运行”。我想我必须以某种方式更新行编辑?但直到现在我还没有弄清楚我怎么能做到这一点。我还将行编辑设置为只读,因为我不希望用户能够更改它,所以也许这是个问题?我认为只读只会影响用户可以做什么。

我在 Qt Designer 中使用 Python3 和 PyQt4。

【问题讨论】:

    标签: python-3.x pyqt pyqt4 qlineedit


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2018-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多