Below is an example of a fully functional “modern” clock built with PyQt. It combines an analog clock (with smooth hand animation and a stylish gradient face) and a digital readout below. You can further modify the colors, sizes, or add more functionality to suit your taste. Save the code as, say, modern_clock.py and run it.
import sys
from PyQt5.QtCore import Qt, QTimer, QTime, QPoint, QRectF
from PyQt5.QtGui import (QPainter, QColor, QPen, QBrush, QPolygon,
QRadialGradient, QLinearGradient, QFont, QFontMetrics)
from PyQt5.QtWidgets import QApplication, QWidget
class ModernClock(QWidget):
def __init__(self, parent=None):
super(ModernClock, self).__init__(parent)
self.setWindowTitle("Modern Clock")
self.resize(400, 450)
# Update clock more frequently for smooth seconds hand animation.
self.timer = QTimer(self)
self.timer.timeout.connect(self.update)
self.timer.start(50)
# Set a dark background style.
self.setStyleSheet("background-color: #2E3440; color: #D8DEE9;")
def paintEvent(self, event):
# Get the widget dimensions.
side = min(self.width(), self.height() - 50) # leave space for digital readout
time_now = QTime.currentTime()
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
# Translate and scale for drawing the clock.
painter.translate(self.width() / 2, (self.height() - 50) / 2)
painter.scale(side / 200.0, side / 200.0)
# ---------------------------------------
# Draw the clock face with a radial gradient.
# ---------------------------------------
gradient = QRadialGradient(0, 0, 100)
gradient.setColorAt(0.0, QColor("#3B4252"))
gradient.setColorAt(1.0, QColor("#2E3440"))
painter.setBrush(QBrush(gradient))
painter.setPen(Qt.NoPen)
painter.drawEllipse(-100, -100, 200, 200)
# ---------------------------------------
# Draw tick marks (longer ticks for hours, shorter for minutes)
# ---------------------------------------
painter.setPen(QPen(QColor("#D8DEE9"), 1))
for i in range(60):
if i % 5 == 0:
painter.drawLine(0, -88, 0, -100)
else:
painter.drawLine(0, -92, 0, -100)
painter.rotate(6) # 360°/60
# ---------------------------------------
# Hour hand
# ---------------------------------------
painter.save()
hour = time_now.hour() + time_now.minute() / 60.0
painter.rotate(30 * hour)
gradientHour = QLinearGradient(0, -4, 0, 40)
gradientHour.setColorAt(0, QColor("#88C0D0"))
gradientHour.setColorAt(1, QColor("#81A1C1"))
painter.setBrush(gradientHour)
# Create a polygon for the hour hand shape.
hour_hand = QPolygon([
QPoint(0, 7),
QPoint(-5, 0),
QPoint(0, -50),
QPoint(5, 0)
])
painter.drawConvexPolygon(hour_hand)
painter.restore()
# ---------------------------------------
# Minute hand
# ---------------------------------------
painter.save()
minute = time_now.minute() + time_now.second() / 60.0
painter.rotate(6 * minute)
gradientMin = QLinearGradient(0, -4, 0, 60)
gradientMin.setColorAt(0, QColor("#8FBCBB"))
gradientMin.setColorAt(1, QColor("#88C0D0"))
painter.setBrush(gradientMin)
minute_hand = QPolygon([
QPoint(0, 7),
QPoint(-3, 0),
QPoint(0, -70),
QPoint(3, 0)
])
painter.drawConvexPolygon(minute_hand)
painter.restore()
# ---------------------------------------
# Second hand with smooth movement
# ---------------------------------------
painter.save()
second = time_now.second() + time_now.msec() / 1000.0
painter.rotate(6 * second)
painter.setPen(Qt.NoPen)
gradientSec = QLinearGradient(0, -4, 0, 80)
gradientSec.setColorAt(0, QColor("#BF616A"))
gradientSec.setColorAt(1, QColor("#D08770"))
painter.setBrush(gradientSec)
second_hand = QPolygon([
QPoint(0, 7),
QPoint(-2, 0),
QPoint(0, -80),
QPoint(2, 0)
])
painter.drawConvexPolygon(second_hand)
painter.restore()
# ---------------------------------------
# Draw a center circle to mask hand anchors.
# ---------------------------------------
painter.setBrush(QColor("#ECEFF4"))
painter.setPen(Qt.NoPen)
painter.drawEllipse(-5, -5, 10, 10)
# ---------------------------------------
# Draw the digital clock readout at the bottom.
# ---------------------------------------
painter.resetTransform()
painter.setPen(QColor("#D8DEE9"))
font = QFont("Helvetica", 20, QFont.Bold)
painter.setFont(font)
time_str = time_now.toString("hh:mm:ss")
fm = QFontMetrics(font)
text_width = fm.horizontalAdvance(time_str)
painter.drawText((self.width() - text_width) / 2, self.height() - 20, time_str)
def main():
app = QApplication(sys.argv)
clock = ModernClock()
clock.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Explanation
-
Smooth Updates:
The QTimer is set to update every 50 milliseconds to ensure smooth animation for the second hand.
-
Custom Design:
- The background uses a dark color and a radial gradient is applied to the clock face.
- The clock hands (hour, minute, second) use linear gradients to give a modern look.
- A digital clock readout is added at the bottom for a different view.
-
Drawing the Clock:
The paintEvent method handles all the drawing:
- We translate and scale the coordinate system for easier drawing.
- Ticks are drawn around the face.
- The clock hands are drawn as polygons, each rotated to the correct angle based on the current time.
This code should run as-is if you have PyQt5 installed. Enjoy and feel free to expand on this “modern” clock design with additional features like alarms, themes, or interactive animations!