Apr-01-2019, 09:10 AM
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 Client
# and add to GUI.py
and interfaces externally.
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.