Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pyqt4 QLineEdit使用其他py模块实时输出_Python_Module_Pyqt4_Real Time - Fatal编程技术网

Python pyqt4 QLineEdit使用其他py模块实时输出

Python pyqt4 QLineEdit使用其他py模块实时输出,python,module,pyqt4,real-time,Python,Module,Pyqt4,Real Time,我想显示如下对话框: 测试0 测试1 测试2 测试3 测试成功 我试着。。。。。但它不起作用 它只显示成功 在执行过程中是否有实时输出的方法 这是示例代码 test.py # -*- coding: utf-8 -*- import sys import test2 from PyQt4 import QtGui class Example(QtGui.QWidget): def __init__(self): super(Example, self).__init

我想显示如下对话框:

测试0

测试1

测试2

测试3

测试成功

我试着。。。。。但它不起作用

它只显示成功

在执行过程中是否有实时输出的方法

这是示例代码

test.py

# -*- coding: utf-8 -*-

import sys
import test2
from PyQt4 import QtGui

class Example(QtGui.QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):      
        self.btn = QtGui.QPushButton('Dialog', self)
        self.btn.move(20, 20)
        self.btn.clicked.connect(self.showDialog)

        self.le = QtGui.QLineEdit(self)
        self.le.move(130, 22)

        self.setGeometry(300, 300, 290, 150)
        self.setWindowTitle('test')
        self.show()

    def showDialog(self):
        self.le.setText(test2.main("10",self.le))

def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()
test2.py

import time

def main(num,text_edit_box):
    for i in range(int(num)):
        text_edit_box.setText(str(i))
        print i
        time.sleep(1)
    return "success"

是的,您可以使用
processEvents
来完成。通常,当代码连续运行时(例如,在
test2.main
中的for循环中),程序从未被告知要更新,因此不会更新
processEvents
是强制GUI更新的简单方法。它必须在
QApplication
类上调用,因此我包含了对(
app
)的引用,该引用被传递到
test2.main

test.py

import sys
import test2
from PyQt4 import QtGui

class Example(QtGui.QWidget):
    def __init__(self, app):
        self.app = app
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):      
        self.btn = QtGui.QPushButton('Dialog', self)
        self.btn.move(20, 20)
        self.btn.clicked.connect(self.showDialog)

        self.le = QtGui.QLineEdit(self)
        self.le.move(130, 22)

        self.setGeometry(300, 300, 290, 150)
        self.setWindowTitle('test')
        self.show()

    def showDialog(self):
        self.le.setText(test2.main("10",self.le, self.app))

def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example(app)
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()
import time

def main(num,text_edit_box, app):
    for i in range(int(num)):
        text_edit_box.setText(str(i))
        print i
        time.sleep(.1)
        app.processEvents()
    return "success"
test2.py

import sys
import test2
from PyQt4 import QtGui

class Example(QtGui.QWidget):
    def __init__(self, app):
        self.app = app
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):      
        self.btn = QtGui.QPushButton('Dialog', self)
        self.btn.move(20, 20)
        self.btn.clicked.connect(self.showDialog)

        self.le = QtGui.QLineEdit(self)
        self.le.move(130, 22)

        self.setGeometry(300, 300, 290, 150)
        self.setWindowTitle('test')
        self.show()

    def showDialog(self):
        self.le.setText(test2.main("10",self.le, self.app))

def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example(app)
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()
import time

def main(num,text_edit_box, app):
    for i in range(int(num)):
        text_edit_box.setText(str(i))
        print i
        time.sleep(.1)
        app.processEvents()
    return "success"
这可能不是最好的方法,但它是使代码正常工作的最简单方法,这取决于您正在尝试的方法


重新绘制GUI的更好的总体方法可能是在GUI类中有一个
update\u GUI
方法,该方法可以由其他模块直接调用,也可以由一个插槽侦听其他模块发送的信号。

谢谢,我可能会把它放在我的简历中;-)