#!/usr/bin/python3 # -*- coding: utf-8 -* #Bertrand Vandeportaele 2019 import os import sys debug=True ######################################## #https://stackoverflow.com/questions/1051254/check-if-python-package-is-installed #install automatically watchdog if not already installed import subprocess import sys reqs = subprocess.check_output([sys.executable, '-m', 'pip', 'freeze']) installed_packages = [r.decode().split('==')[0] for r in reqs.split()] if 'PyQt5' in installed_packages: if debug: print('PyQt5 pip package already installed') else: if debug: print('PyQt5 pip package missing, lets install it') import pip pip.main(['install','PyQt5']) ''' Could not find a version that satisfies the requirement PyQt5-Qt5>=5.15.2 (from PyQt5) (from versions: ) No matching distribution found for PyQt5-Qt5>=5.15.2 (from PyQt5) You are using pip version 9.0.1, however version 21.3.1 is available. You should consider upgrading via the 'pip install --upgrade pip' command. Traceback (most recent call last): File "/home/bvandepo/Bureau/pythonb/staubli/simu_sensor_2_qt5.py", line 32, in from PyQt5.QtGui import * ModuleNotFoundError: No module named 'PyQt5' ''' ######################################## #from PyQt5 import QtCore, QtGui, QtWidgets ''' from PyQt5.QtGui import * from PyQt5.QtNetwork import * from PyQt5.QtCore import * from PyQt5 import * from PyQt5.QtWidgets import * ''' from PyQt5.QtGui import * from PyQt5.QtNetwork import * from PyQt5.QtCore import * from PyQt5.QtWidgets import * from PyQt5 import * import re #pour découpage chaine comme sur https://stackoverflow.com/questions/2175080/sscanf-in-python global capteur0 global capteur1 global capteur2 capteur0=0 capteur1=0 capteur2=0 global slider1Value slider1Value=0 global slider2Value slider2Value=0 ################################################################################ def close(): print('close') exit(0) ################################################################################ def commutTimer(): global timer print('commutTimer') if timer.isActive(): timer.stop() commutTimerButton.setText('start stream sensor') else: timer.start() commutTimerButton.setText('stop stream sensor') ################################################################################ def slider1ValueChanged( value ): #label.setText( str( value ) global slider1Value slider1Value=value print('changed') ################################################################################ def slider2ValueChanged( value ): #label.setText( str( value ) global slider2Value slider2Value=value print('changed') ################################################################################ def bouton0(): global capteur0 capteur0=(capteur0+1)%2 print('capteur0: '+ str(capteur0) +" \n"); ################################################################################ def bouton1(): global capteur1 capteur1=(capteur1+1)%2 print('capteur1: '+ str(capteur1) +" \n"); ################################################################################ def bouton2(): global capteur2 capteur2=(capteur2+1)%2 print('capteur2: '+ str(capteur2) +" \n"); ################################################################################ def sendUDP(i): global capteur0 global capteur1 global capteur2 global udpSocket global destIP global portOut DestIP = QHostAddress(destIP); global slider1Value global slider2Value chaine=str(slider1Value)+ " " + str(slider2Value) chaine=chaine+chr(13)+chr(10) print("la chaine envoyée est: "+chaine) udpSocket.writeDatagram(chaine.encode('utf-8'),DestIP, portOut); ################################################################################ def timerUpdate(): sendUDP(0) ################################################################################ #destIP='192.168.1.50' #pc simulateur #destIP='192.168.0.112' #pc simulateur #destIP='192.168.1.49' #pc serveur rapid connecté en filaire à réseau AIP #destIP='127.0.0.1' #pc serveur sur la boucle locale destIP='172.16.6.59' #pc serveur rapid connecté en filaire à réseau IUT Gomette bleue portOut=10000 posx=10 posy=10 sizex=500 sizey=150 ################################################################################ app = QApplication(sys.argv) w=QWidget(None) #QDialog() statusLabel =QLabel('En attente de datagrammes UDP depuis le PIC32') commutTimerButton=QPushButton('start clignote LED0') quitButton = QPushButton('&Quit') led0Button = QPushButton('Led&0') led1Button = QPushButton('Led&1') led2Button = QPushButton('Led&2') udpSocket =QUdpSocket() udpSocket.bind(portOut, QUdpSocket.ShareAddress) quitButton.clicked.connect(close) commutTimerButton.clicked.connect(commutTimer) led0Button.clicked.connect(bouton0) led1Button.clicked.connect(bouton1) led2Button.clicked.connect(bouton2) ''' app.connect(quitButton,QtCore.SIGNAL('clicked()'), close) app.connect(commutTimerButton, QtCore.SIGNAL('clicked()'), commutTimer) app.connect(led0Button,QtCore.SIGNAL('clicked()'), bouton0) app.connect(led1Button,QtCore.SIGNAL('clicked()'), bouton1) app.connect(led2Button,QtCore.SIGNAL('clicked()'), bouton2) ''' buttonLayout =QHBoxLayout() buttonLayout.addStretch(1) buttonLayout.addWidget(quitButton) buttonLayout.addWidget(commutTimerButton) buttonLayout.addStretch(1) button1Layout =QHBoxLayout() button1Layout.addStretch(1) button1Layout.addWidget(led0Button) button1Layout.addStretch(1) button1Layout.addWidget(led1Button) button1Layout.addStretch(1) button1Layout.addWidget(led2Button) button1Layout.addStretch(1) #http://koor.fr/Python/CodeSamplesQt/PyQtSlider.wp slider1 = QSlider(Qt.Horizontal); slider1.setMinimum(-70) slider1.setMaximum(70) slider1.setGeometry( 10, 10, 600, 40 ) slider1.valueChanged.connect(slider1ValueChanged ) slider1.setValue( 0 ) #labelSlider1Value=QLabel( ) #labelSlider1Value.setGeometry(250, 50, 50, 35) #labelSlider1Value.setText( str( slider1.getValue() ) ) sliderLayout =QHBoxLayout() sliderLayout.addStretch(1) sliderLayout.addWidget(slider1) sliderLayout.addStretch(1) #sliderLayout.addWidget(labelSlider1Value) #sliderLayout.addStretch(1) slider2 = QSlider(Qt.Horizontal); slider2.setMinimum(-70) slider2.setMaximum(70) slider2.setGeometry( 10, 10, 600, 40 ) slider2.valueChanged.connect(slider2ValueChanged ) slider2.setValue( 0 ) sliderLayout.addWidget(slider2) sliderLayout.addStretch(1) mainLayout = QVBoxLayout() mainLayout.addLayout(buttonLayout) mainLayout.addLayout(button1Layout) mainLayout.addLayout(sliderLayout) mainLayout.addWidget(statusLabel) w.setLayout(mainLayout) chaine='@BVDP2019: Port Em '+str(portOut) +' vers IP: '+str(destIP) w.setWindowTitle(chaine) #timer adapted from example 1 of https://www.programcreek.com/python/example/52106/PyQt4.QtCore.QTimer timer = QtCore.QTimer() #app.connect(timer, QtCore.SIGNAL('timeout()'), timerUpdate) timer.timeout.connect(timerUpdate) timer.setInterval(100) timer.start() w.setGeometry(posx,posy,sizex,sizey) #donne le focus au bouton 1 led0Button.setDefault(True) w.show() app.exec_()