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

2024-04-25 17:01:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在Lineedit中显示循环数据,但它没有更新。即使是print命令也不会在终端上打印数据,直到我在lineedit中按下return以外的任何键。请看一下程序并向我建议更改:

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

Tags: 数据fromimportselfletimeinitdef
1条回答
网友
1楼 · 发布于 2024-04-25 17:01:34

在更新QLineEdit的文本后,您需要调用QApplication.instance.processEvents()来强制更新,否则直到最后一个号码,您才会看到任何内容。在

您还需要将textChanged()信号改为textEdited()。使用textChanged()你的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会更好。在

相关问题 更多 >