InterfaceTimer.py 4.23 KB
Newer Older
1
from datetime import datetime
2
from PyQt5 import QtWidgets,QtCore,QtGui
3 4 5 6 7 8 9 10 11 12

class InterfaceTimer(object):

    def __init__(self):
        self.startTimerButton = QtWidgets.QPushButton()
        self.stopTimerButton = QtWidgets.QPushButton()
        self.startRegTimerButton = QtWidgets.QPushButton()
        self.stopRegTimerButton = QtWidgets.QPushButton()
        self.regTimerLabel=QtWidgets.QLabel()

13
        self.timerLabel = QtWidgets.QLabel()
14 15
        self.currentTimeLabel = QtWidgets.QLabel()
        self.currentTimer = QtCore.QTimer()
16 17
        self.regTimer=QtCore.QTimer()
        self.MainWindow=QtWidgets.QDialog()
18

19 20
        self.regTimerLabel.setText("0.0 s")
        self.timerLabel.setText("0.0 s")
21
        self.timeValue=0
22
        self.regTimerValue=1000
23 24 25 26 27 28

    def linkActions(self):
        self.startTimerButton.pressed.connect(self.startScreenTimer)
        self.stopTimerButton.pressed.connect(self.stopScreenTimer)
        self.currentTimer.timeout.connect(self.updateCurrentTime)
        self.startRegTimerButton.pressed.connect(self.startRegressiveTimer)
29
        self.stopRegTimerButton.pressed.connect(self.stopRegressiveTimer)
30 31 32 33 34
        self.currentTimer.start(500)
        self.screenTimerFlag = False

    def startRegressiveTimer(self):
        try:
35 36 37 38 39 40 41 42 43
            if(self.regTimerLabel.text().split(" ",3)[1]=="s"):
                self.regTimerValue=self.regTimerLabel.text().split(" ",3)[0]

            else:
                self.regTimerValue=self.regTimerLabel.text()
            print(self.regTimerValue)
            self.regTimerValue=float(self.regTimerValue)

        except(IndexError):
44
            self.regTimerValue=self.regTimerLabel.text()
45 46 47 48 49 50 51 52 53 54 55

        except(ValueError,TypeError):
            error=QtWidgets.QMessageBox(self.MainWindow)
            error.setText("Valor Inválido! Definindo valor padrão de 1000 segundos")
            error.setWindowTitle("ERRO!")
            error.exec()
            self.regTimerValue = 1000

        print(self.regTimerValue)
        self.regTimerValue = float(self.regTimerValue)

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

        self.regTimer=QtCore.QTimer()
        self.regTimer.timeout.connect(self.updateRegTimer)
        self.regTimer.start(100)
        self.startRegTimerButton.pressed.disconnect()
        self.startRegTimerButton.setText("Pausar")
        self.startRegTimerButton.pressed.connect(self.pauseRegressiveTimer)

    def pauseRegressiveTimer(self):
        self.regTimer.stop()
        self.startRegTimerButton.setText("Continuar")
        self.startRegTimerButton.pressed.disconnect()
        self.startRegTimerButton.pressed.connect(self.startRegressiveTimer)

    def stopRegressiveTimer(self):
        self.regTimer.stop()
        self.startRegTimerButton.setText("Iniciar")
        self.startRegTimerButton.pressed.disconnect()
74
        self.startRegTimerButton.pressed.connect(self.startRegressiveTimer)
75 76 77
        self.regTimerLabel.setText("0.00 s")

    def updateRegTimer(self):
78
        if(self.regTimerValue>=0.1):
79 80
            self.regTimerValue -= 0.1

81
        self.regTimerLabel.setText("%.1f s"%self.regTimerValue)
82 83 84 85 86

    def startScreenTimer(self):
        self.screenTimer = QtCore.QTimer()
        self.screenTimer.timeout.connect(self.updateScreenTimer)
        self.screenTimer.start(100)
87 88 89 90 91 92 93 94 95
        self.startTimerButton.pressed.disconnect()
        self.startTimerButton.pressed.connect(self.pauseScreenTimer)
        self.startTimerButton.setText("Pausar")

    def pauseScreenTimer(self):
        self.screenTimer.stop()
        self.startTimerButton.setText("Continuar")
        self.startTimerButton.pressed.disconnect()
        self.startTimerButton.pressed.connect(self.startScreenTimer)
96 97 98

    def stopScreenTimer(self):
        self.screenTimer.stop()
99
        self.timerLabel.setText("0.00 s")
100 101 102 103 104 105
        self.timeValue = 0
        self.screenTimerFlag = False
        self.startTimerButton.setText("Iniciar")

    def updateScreenTimer(self):
        self.timeValue += 0.1
106
        self.timerLabel.setText("%.1f s" % self.timeValue)
107 108 109 110 111 112

    def updateCurrentTime(self):
        self.currentTime = (str(datetime.now().time()))
        self.currentTime = self.currentTime.split(":", 4)
        self.currentTime[2] = self.currentTime[2].split(".", 3)
        self.currentTimeLabel.setText("%s:%s:%s" % (self.currentTime[0], self.currentTime[1], self.currentTime[2][0]))