Let's code a digital clock with Python! 🕒

Published: 31 July 2024
on channel: Bro Code
21,177
435

#python #pythontutorial #pythoncourseforbeginners

00:00:00 pip install PyQt5
00:00:41 imports
00:01:57 class DigitalClock(QWidget)
00:02:59 if _name_ == "__main__"
00:04:48 setting up __init__()
00:05:29 initUI()
00:10:03 update_time()
00:12:47 custom font

Python PyQt5 Digital Clock
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
from PyQt5.QtCore import QTimer, QTime, Qt
from PyQt5.QtGui import QFont, QFontDatabase

class DigitalClock(QWidget):
def __init__(self):
super().__init__()
self.time_label = QLabel(self)
self.timer = QTimer(self)
self.initUI()

def initUI(self):
self.setWindowTitle("Digital Clock")
self.setGeometry(600, 400, 300, 100)

vbox = QVBoxLayout()
vbox.addWidget(self.time_label)
self.setLayout(vbox)

self.time_label.setAlignment(Qt.AlignCenter)

self.time_label.setStyleSheet("font-size: 150px;"
"color: hsl(111, 100%, 50%);")
self.setStyleSheet("background-color: black;")

Use a custom font
font_id = QFontDatabase.addApplicationFont("DS-DIGIT.TTF")
font_family = QFontDatabase.applicationFontFamilies(font_id)[0]
my_font = QFont(font_family, 300)
self.time_label.setFont(my_font)

self.timer.timeout.connect(self.update_time)
self.timer.start(1000)

self.update_time()

def update_time(self):
current_time = QTime.currentTime().toString("hh:mm:ss AP")
self.time_label.setText(current_time)

if _name_ == "__main__":
app = QApplication(sys.argv)
clock = DigitalClock()
clock.show()
sys.exit(app.exec_())


On this page of the site you can watch the video online Let's code a digital clock with Python! 🕒 with a duration of hours minute second in good quality, which was uploaded by the user Bro Code 31 July 2024, share the link with friends and acquaintances, this video has already been watched 21,177 times on youtube and it was liked by 435 viewers. Enjoy your viewing!