Apr-16-2019, 01:23 PM
#!/usr/bin/python3 import sys, time, socket from PyQt5 import QtWidgets, QtCore HOST = '192.168.1.58' PORT = 80 class WorkerThread(QtCore.QObject): signal = QtCore.pyqtSignal(int) def __init__(self): super().__init__() @QtCore.pyqtSlot() def run(self): while True: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.connect((HOST, PORT)) s.sendall(b'Hello, world') data = s.recv(1024) print('Received distance value', repr(data)) self.signal.emit(int(data)) class WorkerLabel(QtWidgets.QLabel): def __init__(self, parent): super().__init__() @QtCore.pyqtSlot(int) def slot(self, i): self.setText(str(i)) class UserInterface(QtWidgets.QWidget): def __init__(self, parent): super().__init__() self.label = WorkerLabel(self) self.layout = QtWidgets.QVBoxLayout() self.layout.addWidget(self.label) self.setLayout(self.layout) class Main(QtWidgets.QMainWindow): def __init__(self): super().__init__() self.ui = UserInterface(self) self.setCentralWidget(self.ui) self.worker = WorkerThread() self.workerThread = QtCore.QThread() # Move the Worker object to the Thread object self.workerThread.started.connect(self.worker.run) # Init worker run() at startup self.worker.moveToThread(self.workerThread) self.worker.signal.connect(self.ui.label.slot) self.workerThread.start() self.show() if __name__== '__main__': app = QtWidgets.QApplication([]) gui = Main() sys.exit(app.exec_())In this code, GUI client can take "data" from server. But, data is coming in every approximately 5 sec. Because in python code, connecting to server is taking too much time. I thought that I should spare SOCKET lines from while loop. But when i did this, it gave an error that "data is not defined"