Verify internet connection from QThread
Unsolved
General and Desktop
-
I have this small application to which I want to implement the threads, since the interface freezes every time it makes the verification, I was investigating and I managed to apply the threads without error but the interface continues freezing every time it makes the verification, the program works with a timer that executes the verification every 5 seconds.
from PyQt5.QtWidgets import QWidget, QPushButton, QApplication, QListWidget, QGridLayout, QLabel from PyQt5.QtCore import QTimer, QDateTime, QThread import requests import sys class CheckThread(QThread): def __init__(self): super().__init__() def check(self): try: request = requests.get("http://www.google.com", timeout=5) return True except (requests.ConnectionError, requests.Timeout) as exception: return False class WinForm(QWidget): def __init__(self, parent=None): super(WinForm, self).__init__(parent) self.setWindowTitle('QTimer example') self.listFile = QListWidget() self.label = QLabel('Label') self.startBtn = QPushButton('Start') self.endBtn = QPushButton('Stop') self.count = 0 layout = QGridLayout() self.timer = QTimer() self.timer.timeout.connect(self.showTime) layout.addWidget(self.label, 0, 0, 1, 2) layout.addWidget(self.startBtn, 1, 0) layout.addWidget(self.endBtn, 1, 1) self.startBtn.clicked.connect(self.startTimer) self.endBtn.clicked.connect(self.endTimer) self.setLayout(layout) def showTime(self): thread = CheckThread() self.count += 1 time = self.count / 10 self.label.setText(str(time)) if time % 5 == 0: print(thread.check()) def startTimer(self): self.start = True self.timer.start(100) self.startBtn.setEnabled(False) self.endBtn.setEnabled(True) def endTimer(self): self.count = 0 self.timer.stop() self.startBtn.setEnabled(True) self.endBtn.setEnabled(False) self.label.setText("0") if __name__ == '__main__': app = QApplication(sys.argv) form = WinForm() form.show() sys.exit(app.exec_())
-
@Jalkhov Use Qt Signals:
class CheckThread(QThread): statusChanged = pyqtSignal(bool) def run(self): try: request = requests.get("http://www.google.com", timeout=5) self.statusChanged.emit(True) except (requests.ConnectionError, requests.Timeout) as exception: self.statusChanged.emit(False)
class WinForm(QWidget): def __init__(self, parent=None): super(WinForm, self).__init__(parent) self.setWindowTitle("QTimer example") self.startBtn = QPushButton("Start") layout = QGridLayout(self) layout.addWidget(self.label) layout.addWidget(self.startBtn) self.check_thread = CheckThread() self.check_thread.statusChanged.connect(self.handle_status_changed) self.startBtn.clicked.connect(self.start_thread) def start_thread(self): self.check_thread.start() def handle_status_changed(self, status): print(status)
-
Ok, I have adapted the code, it would look like this?
from PyQt5.QtWidgets import QWidget, QPushButton, QApplication, QListWidget, QGridLayout, QLabel from PyQt5.QtCore import QTimer, QDateTime, QThread, pyqtSignal import requests import sys class CheckThread(QThread): statusChanged = pyqtSignal(bool) def run(self): try: request = requests.get("http://www.google.com", timeout=5) self.statusChanged.emit(True) except (requests.ConnectionError, requests.Timeout) as exception: self.statusChanged.emit(False) class WinForm(QWidget): def __init__(self, parent=None): super(WinForm, self).__init__(parent) self.setWindowTitle('QTimer example') self.listFile = QListWidget() self.label = QLabel('Label') self.startBtn = QPushButton('Start') self.endBtn = QPushButton('Stop') self.count = 0 layout = QGridLayout() self.timer = QTimer() self.timer.timeout.connect(self.showTime) layout.addWidget(self.label, 0, 0, 1, 2) layout.addWidget(self.startBtn, 1, 0) layout.addWidget(self.endBtn, 1, 1) # self.startBtn.clicked.connect(self.startTimer) self.endBtn.clicked.connect(self.endTimer) self.setLayout(layout) self.check_thread = CheckThread() self.check_thread.statusChanged.connect(self.handle_status_changed) self.startBtn.clicked.connect(self.start_thread) def start_thread(self): self.start = True self.timer.start(100) self.startBtn.setEnabled(False) self.endBtn.setEnabled(True) def handle_status_changed(self, status): print(status) def showTime(self): self.count += 1 time = self.count / 10 self.label.setText(str(time)) if time % 5 == 0: self.check_thread.start() def endTimer(self): self.count = 0 self.timer.stop() self.startBtn.setEnabled(True) self.endBtn.setEnabled(False) self.label.setText("0") if __name__ == '__main__': app = QApplication(sys.argv) form = WinForm() form.show() sys.exit(app.exec_())
-
@Jalkhov said in Verify internet connection from QThread:
Ok, I have adapted the code, it would look like this?
from PyQt5.QtWidgets import QWidget, QPushButton, QApplication, QListWidget, QGridLayout, QLabel from PyQt5.QtCore import QTimer, QDateTime, QThread, pyqtSignal import requests import sys class CheckThread(QThread): statusChanged = pyqtSignal(bool) def run(self): try: request = requests.get("http://www.google.com", timeout=5) self.statusChanged.emit(True) except (requests.ConnectionError, requests.Timeout) as exception: self.statusChanged.emit(False) class WinForm(QWidget): def __init__(self, parent=None): super(WinForm, self).__init__(parent) self.setWindowTitle('QTimer example') self.listFile = QListWidget() self.label = QLabel('Label') self.startBtn = QPushButton('Start') self.endBtn = QPushButton('Stop') self.count = 0 layout = QGridLayout() self.timer = QTimer() self.timer.timeout.connect(self.showTime) layout.addWidget(self.label, 0, 0, 1, 2) layout.addWidget(self.startBtn, 1, 0) layout.addWidget(self.endBtn, 1, 1) # self.startBtn.clicked.connect(self.startTimer) self.endBtn.clicked.connect(self.endTimer) self.setLayout(layout) self.check_thread = CheckThread() self.check_thread.statusChanged.connect(self.handle_status_changed) self.startBtn.clicked.connect(self.start_thread) def start_thread(self): self.start = True self.timer.start(100) self.startBtn.setEnabled(False) self.endBtn.setEnabled(True) def handle_status_changed(self, status): print(status) def showTime(self): self.count += 1 time = self.count / 10 self.label.setText(str(time)) if time % 5 == 0: self.check_thread.start() def endTimer(self): self.count = 0 self.timer.stop() self.startBtn.setEnabled(True) self.endBtn.setEnabled(False) self.label.setText("0") if __name__ == '__main__': app = QApplication(sys.argv) form = WinForm() form.show() sys.exit(app.exec_())
I placed
self.check_thread.start()
inside the showtime function because that way it is shown at the time interval indicated by the if, I don't know if it is right like that.