Course Hive
Search

Welcome

Sign in or create your account

Continue with Google
or
Code a stopwatch using Python! โฑ
Play lesson

Python tutorial for beginners ๐Ÿ - Code a stopwatch using Python! โฑ

4.0 (0)
8 learners

What you'll learn

This course includes

  • 14.3 hours of video
  • Certificate of completion
  • Access on mobile and TV

Summary

Keywords

Full Transcript

#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_())

Course Hive

Continue this lesson in the app

Install CourseHive on Android or iOS to keep learning while you move.

Related Courses

FAQs

Course Hive
Download CourseHive
Keep learning anywhere