collab/mm20
git clone https://git.julia.blue/collab/mm20
d984372
master
1# -*- coding: utf-8 -*- 2 3# Form implementation generated from reading ui file 'gui1.ui' 4# 5# Created by: PyQt5 UI code generator 5.15.1 6# 7# WARNING: Any manual changes made to this file will be lost when pyuic5 is 8# run again. Do not edit this file unless you know what you are doing. 9 10 11from PyQt5 import QtCore ,QtGui ,QtWidgets 12from PyQt5 .QtCore import QRunnable ,pyqtSlot ,QThreadPool 13 14import time 15import socket 16 17HEADER = 64 18PORT = 65432 19SERVER = socket .gethostbyname (socket .gethostname ()) 20ADDR = (SERVER ,PORT ) 21FORMAT = 'utf-8' 22DISCONNECT_MESSAGE = '!DISCONNECT' 23 24class Worker (QRunnable ): 25 26@pyqtSlot() 27def run (self ): 28 29'Thread start' ) 30# i =1 31# while True: 32# print(i) 33# i+=1 34# time.sleep(.5) 35# if i==20: 36# break 37#function to run through this thread 38 39#live feed from server loop 40##### SERVER ---> CLIENT (LIVE) 41 42Ui_MainWindow .send (f'[ { ADDR } ] Sent from live feed' ,Ui_MainWindow .client ) 43 44# connected = True 45# while connected: 46# msg_length = self.window.client.recv(HEADER).decode(FORMAT) # from client 47# if msg_length: # if msg_length has something 48# msg_length = int(msg_length) 49# msg = self.window.client.recv(msg_length).decode(FORMAT) 50# 51# if msg == DISCONNECT_MESSAGE: 52# connected = False 53# print(f'[SERVER] {msg}') # prints what server sent to client 54# time.sleep(2) 55# 56# self.window.client.send(f"[{ADDR}] Client received from live feed".encode(FORMAT)) 57# 58# print("Live feed ended (Thread end)") 59 60class Ui_MainWindow (object ): 61def __init__ (self ,client = None ): 62self .voltage = 0 63self .client = client 64self .received_message = '' 65# self.threadpool = QThreadPool() 66# print(f'Multithreading with maximum {self.threadpool.maxThreadCount()} threads') 67 68def setupUi (self ,MainWindow ): 69MainWindow .setObjectName ("MainWindow" ) 70MainWindow .resize (829 ,694 ) 71self .centralwidget = QtWidgets .QWidget (MainWindow ) 72self .centralwidget .setObjectName ("centralwidget" ) 73self .button = QtWidgets .QPushButton (self .centralwidget ) 74self .button .setGeometry (QtCore .QRect (190 ,450 ,451 ,101 )) 75font = QtGui .QFont () 76font .setPointSize (36 ) 77self .button .setFont (font ) 78self .button .setAutoFillBackground (False ) 79self .button .setObjectName ("button" ) 80self .label = QtWidgets .QLabel (self .centralwidget ) 81self .label .setGeometry (QtCore .QRect (200 ,140 ,441 ,61 )) 82self .label .setMaximumSize (QtCore .QSize (441 ,16777215 )) 83font = QtGui .QFont () 84font .setPointSize (24 ) 85self .label .setFont (font ) 86self .label .setLayoutDirection (QtCore .Qt .LeftToRight ) 87self .label .setObjectName ("label" ) 88MainWindow .setCentralWidget (self .centralwidget ) 89self .menubar = QtWidgets .QMenuBar (MainWindow ) 90self .menubar .setGeometry (QtCore .QRect (0 ,0 ,829 ,26 )) 91self .menubar .setObjectName ("menubar" ) 92MainWindow .setMenuBar (self .menubar ) 93self .statusbar = QtWidgets .QStatusBar (MainWindow ) 94self .statusbar .setObjectName ("statusbar" ) 95MainWindow .setStatusBar (self .statusbar ) 96 97self .retranslateUi (MainWindow ) 98QtCore .QMetaObject .connectSlotsByName (MainWindow ) 99 100 101def retranslateUi (self ,MainWindow ): 102_translate = QtCore .QCoreApplication .translate 103MainWindow .setWindowTitle (_translate ("MainWindow" ,"MainWindow" )) 104self .button .setText (_translate ("MainWindow" ,"Start Live Feed" )) 105self .label .setText (_translate ("MainWindow" ,"Battery Voltage : 10 V" )) 106 107# adding action to a button 108self .button .clicked .connect (lambda :self .button_pressed ()) 109 110def button_pressed (self ):#update voltage 111# worker = Worker() 112# self.threadpool.start(worker) 113 114self .label .setText (f"Battery Voltage : { self . voltage } V" ) 115self .label .adjustSize () 116self .send (str (self .voltage ),self .client ) 117self .voltage = int (self .received_message [- 1 ]) 118 119 120def send (self ,msg ,client ): 121message = msg .encode (FORMAT ) 122msg_length = len (message ) 123send_length = str (msg_length ).encode (FORMAT ) 124send_length += b' ' * (HEADER - len (send_length )) 125client .send (send_length )#sends header length 126client .send (message )#sends actual message 127 128self .received_message = client .recv (HEADER ).decode (FORMAT ) 129self .received_message ) 130 131 132 133 134 135 136if __name__ == "__main__" : 137import sys 138app = QtWidgets .QApplication (sys .argv ) 139MainWindow = QtWidgets .QMainWindow () 140ui = Ui_MainWindow () 141ui .setupUi (MainWindow ) 142MainWindow .show () 143sys .exit (app .exec_ ())