1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| #!/usr/bin/env python3
# coding: utf-8
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
import os
# L'objet pour gérer mon appli
class myAppli(QApplication):
# Constructeur
def __init__(self, fic, *args, **kwargs):
# Appel méthode objet hérité
super().__init__(*args, **kwargs)
# Le widget principal
self.__mainWidget=myMainWindow(fic)
# __init__()
# Lancement boucle de traitement des évènements Qt
def exec_(self):
# Affichage du widget principal
self.__mainWidget.show()
# Appel méthode objet hérité
return super().exec_()
# exec_()
# class myAppli
# L'objet pour gérer ma fenêtre principale
class myMainWindow(QMainWindow):
# Constructeur
def __init__(self, fic, *args, **kwargs):
# Appel méthode objet hérité
super().__init__(*args, **kwargs)
# La fenêtre sera détruite à sa fermeture
self.setAttribute(Qt.WA_DeleteOnClose)
# La fenêtre principale doit posséder un widget central qui servira ensuite de référent
self.setCentralWidget(QWidget(parent=self))
# Le fichier à afficher
self.__fic=fic
# La zone d'affichage
self.__affich=QLineEdit(parent=self.centralWidget())
self.__affich.setReadOnly(True)
# La vitesse du timer
speed=QSpinBox(parent=self.centralWidget(), valueChanged=self.__slotSpeed)
speed.blockSignals(True)
speed.setRange(1, 10)
speed.setValue(2)
speed.setWrapping(True)
speed.blockSignals(False)
# Le layout d'affichage
layoutAffich=QHBoxLayout()
layoutAffich.addWidget(self.__affich, stretch=1)
layoutAffich.addWidget(speed, stretch=0)
# Le bouton connecté à la fermeture des fenêtres de l'application
quit=QPushButton("Quit", parent=self.centralWidget(), clicked=qApp.closeAllWindows)
# Le gestionnaire de positionnement principal
mainLayout=QVBoxLayout(self.centralWidget())
mainLayout.addLayout(layoutAffich, stretch=0)
mainLayout.addStretch(1)
mainLayout.addWidget(quit, stretch=0)
# Le timer qui activera la lecture du fichier
self.__timer=QTimer(self)
self.__timer.setTimerType(Qt.PreciseTimer)
self.__timer.setInterval(speed.value() * 1000)
self.__timer.timeout.connect(self.__slotTimer)
# __init()
# Lorsque la fenêtre s'affiche
def showEvent(self, event, *args, **kwargs):
# Appel méthode objet hérité
super().showEvent(event, *args, **kwargs)
# Première lecture
self.__slotTimer()
# Lancement timer
self.__timer.start()
# showEvent()
# Lorsque le timer s'active
@pyqtSlot()
def __slotTimer(self):
# Si le fichier est ok
if os.path.isfile(self.__fic):
# On lit le fichier et on l'affiche dans la zone
with open(self.__fic, "r") as fp: self.__affich.setText(fp.read())
else:
# Message info
self.__affich.setText("Erreur, fichier [{}] incorrect ou inexistant".format(self.__fic))
# if
# __slotTimer()
# Lorsque la vitesse change
@pyqtSlot(int)
def __slotSpeed(self, value):
# Le timer s'adapte
print("value={}".format(value))
self.__timer.setInterval(value * 1000)
# __slotSpeed()
# class myMainWindow
if __name__ == "__main__":
# L'application principale Qt
sys.exit(myAppli("toto", sys.argv).exec_())
# if |
Partager