from PyQt5.QtCore import QTimer from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QVBoxLayout, QHBoxLayout import sys class TimerTester(QWidget): def __init__(self, *args, **kwargs): # Inicializace QWidgetu samotného super().__init__(*args, **kwargs) # Hodiny self.clock = QTimer() self.clock.setInterval(10) self.clock.timeout.connect(self.clockTick) self.clock.start() self.clockLabel = QLabel(self) self.ticks = 0 # Čajový časovač na jemný zelený čaj self.tea = QTimer() self.tea.timeout.connect(self.teaTime) self.teaLabel = QLabel(self) self.teaBegin = None self.teaButton = QPushButton(self, text="Louhovat!") self.teaButton.clicked.connect(self.teaBoil) # Stavový řádek self.stateLabel = QLabel(self) # Umístění a zobrazení self.layout = QVBoxLayout(self) self.layout.addWidget(self.clockLabel) self.layout.addWidget(self.teaLabel) self.layout.addWidget(self.teaButton) self.layout.addWidget(self.stateLabel) self.setLayout(self.layout) self.updateLabels() self.show() def clockTick(self): self.ticks += 1 self.updateLabels() def teaBoil(self): self.teaButton.setText("Ještě!") self.tea.setInterval(90000) if self.tea.isActive(): self.stateLabel.setText("Čajovač už je aktivní") else: self.stateLabel.setText("Čajovač nastartován") self.teaBegin = self.ticks self.tea.start() def teaTime(self): QMessageBox("Čaj je hotov!").exec() sys.exit(0) def updateLabels(self): self.clockLabel.setText("Teď je %(time).2fs od začátku" % { "time" : self.ticks / 100 }) if self.teaBegin is not None: self.teaLabel.setText( "Čaj už se louhuje %(elapsed).2fs a bude se louhovat ještě %(remaining).2fs" % { "elapsed": (self.ticks - self.teaBegin) / 100, "remaining": self.tea.remainingTime() / 1000 } ) app = QApplication(sys.argv) tester = TimerTester() app.exec()