#python #pythontutorial #pythoncourse
00:00:00 intro
00:00:16 pip install PyQt5
00:00:37 imports
00:01:56 class Stopwatch(QWidget)
00:02:28 if _name_
00:03:54 START HERE
00:05:57 method declarations
00:06:55 initUI()
00:09:40 setStyleSheet()
00:12:28 signals and slots
00:13:33 methods definitions
Python PyQt5 Stopwatch
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QLabel,
QPushButton, QVBoxLayout, QHBoxLayout)
from PyQt5.QtCore import QTimer, QTime, Qt
class Stopwatch(QWidget):
def __init__(self):
super().__init__()
self.time = QTime(0, 0, 0, 0)
self.time_label = QLabel("00:00:00.00", self)
self.start_button = QPushButton("Start", self)
self.stop_button = QPushButton("Stop", self)
self.reset_button = QPushButton("Reset", self)
self.timer = QTimer(self)
self.initUI()
def initUI(self):
self.setWindowTitle("Stopwatch")
vbox = QVBoxLayout()
vbox.addWidget(self.time_label)
self.setLayout(vbox)
self.time_label.setAlignment(Qt.AlignCenter)
hbox = QHBoxLayout()
hbox.addWidget(self.start_button)
hbox.addWidget(self.stop_button)
hbox.addWidget(self.reset_button)
vbox.addLayout(hbox)
self.setStyleSheet("""
QPushButton, QLabel{
padding: 20px;
font-weight: bold;
font-family: calibri;
}
QPushButton{
font-size: 50px;
}
QLabel{
font-size: 120px;
background-color: hsl(200, 100%, 85%);
border-radius: 20px;
}
""")
self.start_button.clicked.connect(self.start)
self.stop_button.clicked.connect(self.stop)
self.reset_button.clicked.connect(self.reset)
self.timer.timeout.connect(self.update_display)
def start(self):
self.timer.start(10)
def stop(self):
self.timer.stop()
def reset(self):
self.timer.stop()
self.time = QTime(0, 0, 0, 0)
self.time_label.setText(self.format_time(self.time))
def format_time(self, time):
hours = time.hour()
minutes = time.minute()
seconds = time.second()
milliseconds = time.msec() // 10
return f"{hours:02}:{minutes:02}:{seconds:02}.{milliseconds:02}"
def update_display(self):
self.time = self.time.addMSecs(10)
self.time_label.setText(self.format_time(self.time))
if __name__ == "__main__":
app = QApplication(sys.argv)
stopwatch = Stopwatch()
stopwatch.show()
sys.exit(app.exec_())
Watch video Let's code a STOPWATCH in Python! ⏱ online, duration hours minute second in high quality that is uploaded to the channel Bro Code 02 August 2024. Share the link to the video on social media so that your subscribers and friends will also watch this video. This video clip has been viewed 11,197 times and liked it 277 visitors.