execute ping command with GUI

前端 未结 3 1467
刺人心
刺人心 2020-12-12 03:39

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.

相关标签:
3条回答
  • 2020-12-12 03:55

    As an option. Try it:

    import sys
    import os
    from PyQt5.QtWidgets import QApplication,QMessageBox
    from PyQt5.QtCore import QTimer
    
    app = QApplication(sys.argv)
    
    box = QMessageBox()
    box.setText("Text")
    box.show()
    
    def osSystem():
        os.system("ping 8.8.8.8 ") 
    
    QTimer.singleShot(20, osSystem )
    
    #os.system("ping 8.8.8.8 ")
    
    
    sys.exit(app.exec())
    

    0 讨论(0)
  • 2020-12-12 04:16

    Here is a single-line solution:

    from PyQt5.QtWidgets import QApplication,QMessageBox
    import sys
    import os
    app = QApplication(sys.argv)
    
    box = QMessageBox()
    box.setText("Text")
    box.show()
    QApplication.processEvents() # <------------ this one
    os.system("ping 8.8.8.8 ")
    
    sys.exit(app.exec())
    
    0 讨论(0)
  • 2020-12-12 04:20

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

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

    Update

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

    Another Option:

    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)
    
    0 讨论(0)
提交回复
热议问题