Python Forum

Full Version: A dynamically updating GUI screen from URL
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
(Apr-10-2019, 04:00 PM)Alfalfa Wrote: [ -> ]Hard to tell, but you can easily put them in the same class. Simply make one blocking method for the initial connection and call it before the while loop. If the connection is successful, return True and run the loop until the connection is closed.
Do you have an example code Think
Something like this perhaps?

#!/usr/bin/python3
import sys
from PyQt5 import QtWidgets, QtCore

class WorkerThread(QtCore.QObject):
    output = QtCore.pyqtSignal(int)

    def __init__(self):
        super().__init__()
        self.timer = QtCore.QTimer(interval=1000)
        self.timer.timeout.connect(self._run)
        self.timer.start()
        self.connected = False

    def _connect(self):
        success = True ##
        if success:
            self.connected = True

    def _parse(self):
        alive = True ##
        if alive:
            self.output.emit(1337)
        else:
            self.connected = False

    def _run(self):
        if self.connected:
            self._parse()
        else:
            self._connect()


class Main(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.worker = WorkerThread()
        self.workerThread = QtCore.QThread()
        self.worker.moveToThread(self.workerThread)
        self.worker.output.connect(self.output)
        self.workerThread.start()

    def output(self, data):
        print(data)

if __name__== '__main__':
    app = QtWidgets.QApplication([])
    gui = Main()
    sys.exit(app.exec_())
Thanks your super fast reply :) Can you click this link? Maybe i can take help from here
(Apr-15-2019, 02:30 PM)Alfalfa Wrote: [ -> ]Something like this perhaps?

#!/usr/bin/python3
import sys
from PyQt5 import QtWidgets, QtCore

class WorkerThread(QtCore.QObject):
    output = QtCore.pyqtSignal(int)

    def __init__(self):
        super().__init__()
        self.timer = QtCore.QTimer(interval=1000)
        self.timer.timeout.connect(self._run)
        self.timer.start()
        self.connected = False

    def _connect(self):
        success = True ##
        if success:
            self.connected = True

    def _parse(self):
        alive = True ##
        if alive:
            self.output.emit(1337)
        else:
            self.connected = False

    def _run(self):
        if self.connected:
            self._parse()
        else:
            self._connect()


class Main(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.worker = WorkerThread()
        self.workerThread = QtCore.QThread()
        self.worker.moveToThread(self.workerThread)
        self.worker.output.connect(self.output)
        self.workerThread.start()

    def output(self, data):
        print(data)

if __name__== '__main__':
    app = QtWidgets.QApplication([])
    gui = Main()
    sys.exit(app.exec_())

I tried this. But this code is not related to "blocking method". Right ?
You would have to replace the lines marked with ## with your blocks of code. For instance, "success = True ##" would be a blocking step to establish the initial connection, then if it succeed, it set the "self.connected" flag to true so it can proceed with fetching and/or parsing. It is pretty basic stuff, which can be done in many different ways. It is up to you to figure out the best way to implement it according to your needs.
#!/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"
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.
(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.
The error is caused by the indentation, simply remove "@QtCore.pyqtSlot()" which is useless there, and reduce the indentation of lines 14 and 15 so it is coherent with the rest of your code.
(Apr-17-2019, 06:32 PM)Alfalfa Wrote: [ -> ]The error is caused by the indentation, simply remove "@QtCore.pyqtSlot()" which is useless there, and reduce the indentation of lines 14 and 15 so it is coherent with the rest of your code.

import sys, time, socket
from PyQt5 import QtWidgets, QtCore
HOST = '192.168.1.38'  # The server's hostname or IP address, NodeMCU IP Address
PORT = 80              # The port used by the server, PORT NUMBER in ARDUINO CODE

class WorkerThread(QtCore.QObject):
    signal = QtCore.pyqtSignal(int)
 
    def __init__(self):
        super().__init__()

    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))   
        
Error:
Traceback (most recent call last): File "c:/Users/User/Desktop/PYQT/work_1.py", line 20, in run data = s.recv(1024) NameError: name 's' is not defined
I tried Huh
Pages: 1 2 3