Python Forum
How to combine data taken from server between client and GUI?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to combine data taken from server between client and GUI?
#1
I'm reaching a server which takes distance data from ultrasonic sensor. (Server was created at another side with Arduino.) I have to show this data on Raspberry Pi, so I want to do GUI. I have two .py file.

1) Client module

2) GUI

I want to take data on client over GUI code. Data will be taken from URL WebPage (TCP/IP)
Where/How I should add data taken from server to GUI code? (Embedding client. py to GUI.py)
Any suggestions would be greatly appreciated

Client.py

import socket
HOST = '192.168.1.59'  # The server's hostname or IP address, NodeMCU IP Address
PORT = 80              # The port used by the server, PORT NUMBER in ARDUINO CODE 

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((HOST, PORT))
    s.sendall(b'Hello, world')
    data = s.recv(1024)
GUI.py

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(30, 30, 600, 300))
        font = QtGui.QFont()
        font.setFamily("Calibri")
        font.setPointSize(20)
        self.label.setFont(font)
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName("label")
        self.labelDistance = QtWidgets.QLabel(self.centralwidget)
        self.labelDistance.setGeometry(QtCore.QRect(150, 150, 400, 300))
        font = QtGui.QFont()
        font.setFamily("Calibri")
        font.setPointSize(20)
        self.labelDistance.setFont(font)
        self.labelDistance.setAlignment(QtCore.Qt.AlignCenter)
        self.labelDistance.setObjectName("labelDistance")
        self.labelcm = QtWidgets.QLabel(self.centralwidget)
        self.labelcm.setGeometry(QtCore.QRect(150, 150, 700, 300))
        font = QtGui.QFont()
        font.setFamily("Calibri")
        font.setPointSize(20)
        self.labelcm.setFont(font)
        self.labelcm.setAlignment(QtCore.Qt.AlignCenter)
        self.labelcm.setObjectName("labelcm")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 259, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Distance Measurement"))
        self.label.setText(_translate("MainWindow", "Obsctacle Distance"))
        self.labelDistance.setText(_translate("MainWindow", "0"))
        self.labelcm.setText(_translate("MainWindow", "cm"))
        
When GUI code runs: (0 is default value)

[Image: 9a2Co.png]
Reply
#2
If you want to keep client.py in a separate module, you need to provide some sort of interface to the data.
You can do this by wrapping your code in a function, and returning data, or by making client.py a class and exposing data
for outside use in the __init__ method, by defining data as self.data.

as a class:
Client.py
import socket

class Client:
    def __init__(self):
        self.HOST = '192.168.1.59'  # The server's hostname or IP address, NodeMCU IP Address
	    self.PORT = 80              # The port used by the server, PORT NUMBER in ARDUINO CODE

    def transfer_data(self):
        s.connect((HOST, PORT))
        s.sendall(b'Hello, world')
        self.data = s.recv(1024)

    
# To try run stand-alone
if __name__ == '__main__':
    cl = Client()
    cl.transfer_data()
    print(cl.data)
as a function:
import socket
HOST = '192.168.1.59'  # The server's hostname or IP address, NodeMCU IP Address
PORT = 80              # The port used by the server, PORT NUMBER in ARDUINO CODE 
 
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((HOST, PORT))
    s.sendall(b'Hello, world')
    return s.recv(1024)
Now in the GUI class:
import Client

# and add to GUI.py
class Ui_MainWindow(object):
    def __init__(self):
        self.client = Client.Client()
        self.cdata = self.client.transfer_data()
# then when needed:
        data = self.client.data()
A better way would to have a main module that imports both GUI and Client
and interfaces externally.
Reply
#3
(Apr-01-2019, 09:10 AM)Larz60+ Wrote: A better way would to have a main module that imports both GUI and Client
and interfaces externally.

Yes i want to create a main module that includes GUI and Client. And also, data should be taken dynamically and GUI should show dynamically, you estimate that distance is always changing.
Reply
#4
Quote:as a class:
Client.py
import socket

class Client:
    def __init__(self):
        self.HOST = '192.168.1.59'  # The server's hostname or IP address, NodeMCU IP Address
	    self.PORT = 80              # The port used by the server, PORT NUMBER in ARDUINO CODE

    def transfer_data(self):
        s.connect((HOST, PORT))
        s.sendall(b'Hello, world')
        self.data = s.recv(1024)

    
# To try run stand-alone
if __name__ == '__main__':
    cl = Client()
    cl.transfer_data()
    print(cl.data)

İmage


I tried. What should i do ?
Reply
#5
Quote:I tried. What should i do ?
show what you've tried
Reply
#6
Quote:show what you've tried

I attached a photo to previous reply. Don't view it ? I'm adding code now.

import socket
 
class client:
    def __init__(self):
        self.HOST = '192.168.1.55'  # The server's hostname or IP address, NodeMCU IP Address
        self.PORT = 80              # The port used by the server, PORT NUMBER in ARDUINO CODE
 
    def transfer_data(self):
        s.connect((HOST, PORT))
        s.sendall(b'Hello, world')
        self.data = s.recv(1024)
 
# To try run stand-alone
if __name__ == '__main__':
    cl = client()
    cl.transfer_data()
    print(cl.data)
    
Error:
> & "C:/Program Files/Python37/python.exe" c:/Users/User/Desktop/PYQT/client.py Traceback (most recent call last): File "c:/Users/User/Desktop/PYQT/client.py", line 16, in <module> cl.transfer_data() File "c:/Users/User/Desktop/PYQT/client.py", line 9, in transfer_data s.connect((HOST, PORT)) NameError: name 's' is not defined
Reply
#7
My mistake:
import socket
 
class Client:
    def __init__(self):
        self.HOST = '192.168.1.59'  # The server's hostname or IP address, NodeMCU IP Address
        self.PORT = 80              # The port used by the server, PORT NUMBER in ARDUINO CODE
 
    def transfer_data(self):
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
            s.connect((HOST, PORT))
            s.sendall(b'Hello, world')
            self.data = s.recv(1024)
 
     
# To try run stand-alone
if __name__ == '__main__':
    cl = Client()
    cl.transfer_data()
    print(cl.data)
Reply
#8
I added HOST and PORT definition, because when there isnt them, it gave an error.
import socket
HOST = '192.168.1.62'
PORT = 80 

class client:
    def __init__(self):
        self.HOST = '192.168.1.62'  # The server's hostname or IP address, NodeMCU IP Address
        self.PORT = 80              # The port used by the server, PORT NUMBER in ARDUINO CODE
 
    def transfer_data(self):
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
            s.connect((HOST, PORT))
            s.sendall(b'Hello, world')
            self.data = s.recv(1024)
 
# To try run stand-alone
if __name__ == '__main__':
    cl = client()
    cl.transfer_data()
    print(cl.data)
I could take output, thanks your solution. Can you share with me cause of "b" ? When i delete it, running was unsuccesful.
Output:
& "C:/Program Files/Python37/python.exe" c:/Users/User/Desktop/PYQT/client.py b'0'

(Apr-01-2019, 09:10 AM)Larz60+ Wrote: Now in the GUI class:
import Client

# and add to GUI.py
class Ui_MainWindow(object):
    def __init__(self):
        self.client = Client.Client()
        self.cdata = self.client.transfer_data()
# then when needed:
        data = self.client.data()
A better way would to have a main module that imports both GUI and Client
and interfaces externally.

Actually, i want to create as a function. But i tried as a class.
from PyQt5 import QtCore, QtGui, QtWidgets
import client 

class Ui_MainWindow(object):

    def __init__(self):
        self.client = client.client()
        self.cdata = self.client.transfer_data()
# then when needed:
        data = self.client.data()
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(30, 30, 600, 300))
        font = QtGui.QFont()
        font.setFamily("Calibri")
        font.setPointSize(20)
        self.label.setFont(font)
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName("label")
        self.labelDistance = QtWidgets.QLabel(self.centralwidget)
        self.labelDistance.setGeometry(QtCore.QRect(150, 150, 400, 300))
        font = QtGui.QFont()
        font.setFamily("Calibri")
        font.setPointSize(20)
        self.labelDistance.setFont(font)
        self.labelDistance.setAlignment(QtCore.Qt.AlignCenter)
        self.labelDistance.setObjectName("labelDistance")
        self.labelcm = QtWidgets.QLabel(self.centralwidget)
        self.labelcm.setGeometry(QtCore.QRect(150, 150, 700, 300))
        font = QtGui.QFont()
        font.setFamily("Calibri")
        font.setPointSize(20)
        self.labelcm.setFont(font)
        self.labelcm.setAlignment(QtCore.Qt.AlignCenter)
        self.labelcm.setObjectName("labelcm")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 259, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Distance Measurement"))
        self.label.setText(_translate("MainWindow", "Obstacle Distance"))
        self.labelDistance.setText(_translate("MainWindow", "0"))
        self.labelcm.setText(_translate("MainWindow", "cm"))
        

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
Error:
PS C:\Users\User> & "C:/Program Files/Python37/python.exe" c:/Users/User/Desktop/PYQT/GUI.py Traceback (most recent call last): File "c:/Users/User/Desktop/PYQT/GUI.py", line 64, in <module> ui = Ui_MainWindow() File "c:/Users/User/Desktop/PYQT/GUI.py", line 10, in __init__ data = self.client.data() TypeError: 'bytes' object is not callable


When i try as a function.

from PyQt5 import QtCore, QtGui, QtWidgets
import socket
HOST = '192.168.1.62'  # The server's hostname or IP address
PORT = 80        # The port used by the server

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((HOST, PORT))
    s.sendall(b'Hello, world')
    data = s.recv(1024)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(259, 124)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(30, 10, 211, 41))
        font = QtGui.QFont()
        font.setFamily("Calibri")
        font.setPointSize(20)
        self.label.setFont(font)
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName("label")
        self.labelDistance = QtWidgets.QLabel(self.centralwidget)
        self.labelDistance.setGeometry(QtCore.QRect(60, 50, 71, 41))
        font = QtGui.QFont()
        font.setFamily("Calibri")
        font.setPointSize(20)
        self.labelDistance.setFont(font)
        self.labelDistance.setAlignment(QtCore.Qt.AlignCenter)
        self.labelDistance.setObjectName("labelDistance")
        self.labelcm = QtWidgets.QLabel(self.centralwidget)
        self.labelcm.setGeometry(QtCore.QRect(120, 50, 71, 41))
        font = QtGui.QFont()
        font.setFamily("Calibri")
        font.setPointSize(20)
        self.labelcm.setFont(font)
        self.labelcm.setAlignment(QtCore.Qt.AlignCenter)
        self.labelcm.setObjectName("labelcm")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 259, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Distance Measurement"))
        self.label.setText(_translate("MainWindow", "Obsctacle Distance"))
        self.labelDistance.setText(_translate("MainWindow", "0"))
        self.labelcm.setText(_translate("MainWindow", "cm"))
        # User Code
        self.check_event()

    def check_event(self):
                self.labelDistance.setText(data)
            

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
Error:
PS C:\Users\User> & "C:/Program Files/Python37/python.exe" c:/Users/User/Desktop/PYQT/obstacle_distance_data_DENEME.py Traceback (most recent call last): File "c:/Users/User/Desktop/PYQT/obstacle_distance_data_DENEME.py", line 79, in <module> ui.setupUi(MainWindow) File "c:/Users/User/Desktop/PYQT/obstacle_distance_data_DENEME.py", line 58, in setupUi self.retranslateUi(MainWindow) File "c:/Users/User/Desktop/PYQT/obstacle_distance_data_DENEME.py", line 68, in retranslateUi self.check_event() File "c:/Users/User/Desktop/PYQT/obstacle_distance_data_DENEME.py", line 71, in check_event self.labelDistance.setText(data) TypeError: setText(self, str): argument 1 has unexpected type 'bytes'
As you see, I dont sure what I do.
Reply
#9
line 10:
data = self.client.data()
should not contain ()
the () at the end tells python to execute a method or function.
Here you just want to fetch the data, so:
data = self.client.data
Looks like there are other variables this is done with also
Reply
#10
Thanks for your help, i found another solution :) Now, i can take data from IP and show it. But, this is not dynamic. I see constant value. When distance measurement on IP change, display widget should show new data result. On my program, you can next distance result, only when run python code again. My aim is that GUI should have refresh screen. How it is possible ? (I mean that it should be updating like a clock.)

İmage
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Paramiko Server -- Exception (server): Error reading SSH protocol banner ujlain 3 4,272 Jul-24-2023, 06:52 AM
Last Post: Gribouillis
  trying to stream data from a websocket to client tomtom 2 1,980 Jan-12-2023, 03:01 PM
Last Post: Vadanane
  Client/Server proper finalizing transfer wolfman5874 1 1,421 Jul-04-2022, 07:35 PM
Last Post: wolfman5874
Bug Problem connecting TLS client written in C++ and Twisted server gpropf 0 1,359 Jun-12-2022, 05:57 PM
Last Post: gpropf
  Server/client basic communication ebolisa 0 2,009 Sep-30-2021, 12:22 PM
Last Post: ebolisa
  Client server Multithreading Anan 6 5,750 Apr-21-2021, 08:19 PM
Last Post: SheeppOSU
Question Trouble with Client/Server reverse Shell! Gilush 0 2,755 Feb-03-2021, 01:04 PM
Last Post: Gilush
  Basic client server code question swisscheese 4 3,191 Dec-12-2020, 08:51 AM
Last Post: Larz60+
  How can i create a server for already existing client using Python? Chapanson 21 7,311 Aug-19-2020, 09:12 AM
Last Post: DeaD_EyE
  Simple TCP Client and TCP Server Problem Vapulabis 5 4,334 Jul-12-2020, 05:09 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020