繁体   English   中英

Python Pyqt:无法在终端窗口中的lineedit和print命令中显示数据

[英]Python Pyqt: could not display data in lineedit and print command in terminal window

我正在尝试在Lineedit中显示循环数据,但未更新。 直到我按下除lineedit返回之外的任何键,甚至print命令都不会在终端上打印数据。 看一下程序,并向我建议更改:

import sys
import time
from PyQt4.QtGui import *
from PyQt4.QtCore import *

class MyFrame(QWidget):
def __init__(self):
    QWidget.__init__(self)

    self.le = QLineEdit(self)
    self.le.setGeometry(200,200,75,35)

    i=0
    self.le.setText(str(i))

    self.connect(self.le, SIGNAL("textChanged(QString)"),self.updatedvalue)

def updatedvalue(self):

    for i in range(1,5):
        self.le.setText(str(i))
        print(i)
        time.sleep(1)

app=QApplication(sys.argv)
f=MyFrame()
f.show()
app.exec_()

在更新QLineEdit的文本以强制更新之后,您需要调用QApplication.instance.processEvents() ,否则直到最后一个数字您都看不到任何东西。

您还需要将textChanged()信号更改为textEdited() 使用textChanged() ,一旦调用setText()就会在循环的第一遍再次调用updatedvalue()函数,因为您正在更新QLineEdit的文本。 如果以编程方式更新文本,则不会触发textEdited()信号。

import sys
import time
from PyQt4.QtGui import *
from PyQt4.QtCore import *

class MyFrame(QWidget):
    def __init__(self):
        QWidget.__init__(self)

        self.le = QLineEdit(self)
        self.le.setGeometry(200,200,75,35)

        i = 0
        self.le.setText(str(i))

        self.connect(self.le, SIGNAL("textEdited(QString)"),self.updatedvalue)

    def updatedvalue(self):
        for i in range(1,5):
            self.le.setText(str(i))
            QApplication.instance().processEvents()
            print(i)
            time.sleep(1)

app=QApplication(sys.argv)
f=MyFrame()
f.show()
app.exec_()

正如Bob提到的,使用QTimer会更好。

暂无
暂无

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

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