我会尽量弄清楚的。在
我有一个控制电源的非常简单的测试脚本,该脚本测量来自被测安捷伦电源+单元的一些电流,然后,脚本将这些读数打印如下:
PS.write(b"MEAS:CURR? \n")
time.sleep(2)
response = PS.read(1000)
time.sleep(3)
print(response)
(float(response)*1)
E3632A=(float(response)*1)
print (E3632A)
当脚本执行“打印命令”(print(E3632A)时,所有信息都显示在py.exe文件“DOS窗口(C:\Windows\py.exe文件). 这是我的问题
如何将其嵌入到一个简单的GUI中?我希望我的图形用户界面显示py.exe文件正在显示。很简单。。。我在网上看过所有的帖子,但没有一个真正的解决办法。在
假设您调用的进程是长时间运行的,并且不会一次性生成所有输出,这意味着您不能使用
subprocess.Popen.communicate()
,因为它的目的是读取文件末尾的所有输出。在你必须使用其他标准技术来读取管道。在
由于您希望将其与GUI集成,并且该过程是长期运行的,因此您需要将读取其输出与GUI的主循环相协调。这使事情有些复杂。在
特金特
首先假设您想要使用TkInter,如您的一个例子所示。这给我们带来了几个问题:
root.update()
来破解一个定制的主循环,让我们用线程来解决本应是基于事件的方法。在event_generate()
缺少Tk随事件一起发送用户数据的功能,因此我们不能使用TkInter事件将接收到的输出从一个线程传递到另一个线程。在因此,我们将使用线程来处理它(即使我不想这样做),其中主线程控制TKGUI,辅助线程读取进程的输出,并且TkInter中缺少本地方法来传递数据,我们使用线程安全的队列。在
由于前面提到的警告,TkInter代码最终有点偏大。在
PyQt公司
相反,使用PyQt可以大大改善我们的情况,因为该框架已经提供了一种与子流程集成的本地方法,其形式是其QProcess类。在
这意味着我们可以去掉线程,而使用Qt的原生信号和槽机制。在
^{pr2}$我们最终得到了一个从Qt类派生的小样板,但是使用了一个整体更干净的方法。在
一般注意事项
还要确保您调用的进程没有缓冲多个输出行,否则它看起来仍然像是控制台卡住了。在
特别是如果被调用方是python程序,您可以确保它使用
print(..., flush=True)
,或者用python -u callee.py
调用它来强制执行无缓冲输出。在相关问题 更多 >
编程相关推荐