使用 GUI 执行 ping 命令 [英] execute ping command with GUI

查看:52
本文介绍了使用 GUI 执行 ping 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 PyQt5 中编写 GUI 应用程序时,我遇到了奇怪的(对我而言)行为.当我想打开一个信息窗口并在它完全加载后开始做另一件事时.我注意到信息窗口在下一个代码块完成之前不会完全加载.

While writing a GUI application in PyQt5 I encounter weird(for me) behavior. When I wanted to open an information window and start doing another thing after it fully loads. I noticed that the information window does not load fully until the next block of code is done.

这就是它的样子

重现这种不需要的行为的代码

from PyQt5.QtWidgets import QApplication,QMessageBox
import sys
import os
app = QApplication(sys.argv)

box = QMessageBox()
box.setText("Text")
box.show()
os.system("ping 8.8.8.8 ")


sys.exit(app.exec())

无论我使用QMessegBox,在另一个类中继承它还是编写我自己的QMeesgeBox类型类,行为都是一样的.

Behavior is the same whether I use QMessegBox, inherit it in another class or write my own QMeesgeBox type class.

我想这种行为是因为 os.system() 函数而起作用的,我会使用 Process 或 Thread 来解决方法,但如果可能的话,我想确保窗口已完全加载,然后执行下一个过程正在发生.

I guess this behavior works like this because of os.system() function and I would use Process or Thread to make a workaround, but if It is possible I would like to ensure that window is fully loaded and then the next procedure is taking place.

Python 3.7.0 版

Python version 3.7.0

PyQt5 版本 5.12.1

PyQt5 version 5.12.1

推荐答案

虽然 S.Nick 和 Guimoute 的解决方案看似有效,但实际情况是它只是让窗口显示了片刻但如果你想与你会看到它被冻结了,例如尝试移动窗口来检查它.os.system() 任务被阻塞,所以它必须在另一个线程中执行

Although the solutions of S.Nick and Guimoute seems to work but the reality is that it has only made the window show a moment but if you want to interact with it you will see that it is frozen, for example try to move the window to check it. The os.system() task is blocking so it must be executed in another thread

import os
import sys
from PyQt5.QtWidgets import QApplication,QMessageBox

import threading
app = QApplication(sys.argv)

box = QMessageBox()
box.setText("Text")
box.show()

def task():
    os.system("ping 8.8.8.8 ") 
threading.Thread(target=task, daemon=True).start()
# or threading.Thread(target=os.system, args=("ping 8.8.8.8 ",), daemon=True).start()
sys.exit(app.exec_())

或者使用 QProcess:

Or use QProcess:

import sys
import os
from PyQt5.QtWidgets import QApplication,QMessageBox
from PyQt5.QtCore import QProcess

app = QApplication(sys.argv)

box = QMessageBox()
box.setText("Text")
box.show()

def on_readyReadStandardOutput():
    print(process.readAllStandardOutput().data().decode(), end="")

process = QProcess()
process.start("ping", ["8.8.8.8"])
process.readyReadStandardOutput.connect(on_readyReadStandardOutput)
sys.exit(app.exec_())

更新

import os
import sys
from PyQt5 import QtCore, QtWidgets


class PingObject(QtCore.QObject):
    finished = QtCore.pyqtSignal()

    @QtCore.pyqtSlot()
    def start(self):
        os.system("ping 8.8.8.8")
        self.finished.emit()


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)

    box = QtWidgets.QMessageBox()
    box.setText("Text")
    box.show()
    thread = QtCore.QThread()
    thread.start()
    ping = PingObject()
    ping.moveToThread(thread)
    QtCore.QTimer.singleShot(0, ping.start)
    loop = QtCore.QEventLoop()
    ping.finished.connect(loop.quit)
    loop.exec_()
    print("finished ping")
    sys.exit(app.exec_())

另一种选择:

import os
import sys
from PyQt5 import QtCore, QtWidgets


class Thread(QtCore.QThread):
    def run(self):
        response = os.popen("ping 8.8.8.8")
        for line in response.readlines():
            print(line)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)

    box = QtWidgets.QMessageBox()
    box.setText("Text")
    box.show()

    thread = Thread()
    thread.start()
    ret = app.exec_()
    thread.quit()
    thread.wait()
    sys.exit(ret)

这篇关于使用 GUI 执行 ping 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆