【问题标题】:How I can run while loop with PyQt5如何使用 PyQt5 运行 while 循环
【发布时间】:2019-06-09 23:43:25
【问题描述】:

我在一个项目上工作:程序下载但我在使用 while 循环时遇到问题,用于检查与 Internet 的连接,如果 true 不 setText('') to lable 和 if Flase setText('anyText') to lable

连接检查方法

    def checkInternetConnection(self,host="8.8.8.8", port=53, timeout=3):

    while self.conection==False:
        try:
            socket.setdefaulttimeout(timeout)
            socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
            self.conection = True
            return self.conection

        except Exception as e:
                print(e)
                self.label_9.setText('Please Check Internect Connection')
                self.conection = False
                return self.conection
    self.finished.emit()

我已经厌倦了 QThread 。请问我该怎么做:)?如果连接丢失=False setText('check internet') 以及连接变为 true 时,应用程序正在运行时 setText('')

构造者

From_Class,_=loadUiType(os.path.join(os.path.dirname(__file__),'designe.ui'))
class mainApp(QMainWindow,From_Class):
    finished = pyqtSignal()
    def __init__(self,parent=None):
        super(mainApp, self).__init__(parent)
        QMainWindow.__init__(self)
        super().setupUi(self)
        self.handleGui()
        self.handleButton()
        self.setWindowIcon(QIcon('mainIcon.png'))
        self.menuBarW()
        self.conection = False

主代码

def main():
    app = QApplication(sys.argv)
    window = mainApp()
    window.checkInternetConnection()
    window.show()
    app.exec()

if __name__=='__main__':
    main()

【问题讨论】:

    标签: python multithreading user-interface pyqt5


    【解决方案1】:

    不要对QThread复杂,使用线程库:

    def main():
        app = QtWidgets.QApplication(sys.argv)
        window = mainApp()
        threading.Thread(target=window.checkInternetConnection, daemon=True).start()
        window.show()
        app.exec()
    

    另一方面,由于您使用的是线程,因此您不应该从另一个线程更新 GUI,为此您可以使用QMetaObject::invokeMethod:

    def checkInternetConnection(self,host="8.8.8.8", port=53, timeout=3):
        while True:
            try:
                socket.setdefaulttimeout(timeout)
                socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
                self.conection = True
            except Exception as e:
                self.conection = False
                print(e)
            msg = "" if self.conection else 'Please Check Internect Connection'
            print("msg", msg)
            QtCore.QMetaObject.invokeMethod(self.label_9, "setText",
                QtCore.Qt.QueuedConnection,  
                QtCore.Q_ARG(str, msg))
        self.finished.emit()
    

    【讨论】:

    • 谢谢兄弟,但我希望它方法 checkInternetConnection 在应用程序显示时正在运行,所以如果连接丢失 false setText('Please ..') 并且如果连接回来 setText('' ) :)
    • @Mo7ammed7amad 好的,我是根据你的逻辑,但是,嘿,我会纠正它
    • 谢谢兄弟,但有一个问题,当第一次运行代码时运行良好,但我尝试在应用程序运行时断开与互联网的连接并且消息不会显示消息:)有什么解决办法。谢谢
    • @Mo7ammed7amad 我已经测试过了:1)我通过互联网连接启动了应用程序,然后我停用了互联网并出现了消息,如果我激活了连接,消息就会消失。 2)在没有互联网连接的情况下启动应用程序,因此出现消息,然后激活互联网并且消息消失,如果我停用连接,消息会重新出现。我认为问题出在其他地方(注意:我已删除以下行:self.handleGui() ... self.menuBarW(),因为它们与我的测试无关)
    猜你喜欢
    • 2018-09-27
    • 1970-01-01
    • 1970-01-01
    • 2020-03-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多