Apr-17-2019, 01:16 PM
(Apr-16-2019, 02:55 PM)Alfalfa Wrote: Here you define data with "data = s.recv(1024)". When you move the code related to connection into it's own method, make sure you still define 'data' before trying to emit the signal. In other words, don't emit the signal unless the data variable exist.
import sys, time, socket from PyQt5 import QtWidgets, QtCore HOST = '192.168.1.33' PORT = 80 class WorkerThread(QtCore.QObject): signal = QtCore.pyqtSignal(int) def __init__(self): super().__init__() @QtCore.pyqtSlot() with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.connect((HOST, PORT)) s.sendall(b'Hello, world') def run(self): while True: data = s.recv(1024) print('Received distance value', repr(data)) self.signal.emit(int(data)) #time.sleep(0.1)
Error: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
^
IndentationError: unexpected indent
It should give data result in while with timer, but it should not try to connect server every while loop.