Skip to content

Instantly share code, notes, and snippets.

@encukou
Last active January 8, 2021 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save encukou/4e7382206b61e8d7aec307aad8ee340a to your computer and use it in GitHub Desktop.
Save encukou/4e7382206b61e8d7aec307aad8ee340a to your computer and use it in GitHub Desktop.
# Shaped clock, adapted from:
# https://doc.qt.io/qt-6/qtwidgets-widgets-shapedclock-example.html
# pip install pyside6
from PySide6 import QtWidgets, QtCore, QtGui
app = QtWidgets.QApplication([])
class ShapedClock(QtWidgets.QWidget):
def __init__(self):
super().__init__(None, QtCore.Qt.FramelessWindowHint | QtCore.Qt.WindowSystemMenuHint)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
timer = QtCore.QTimer(self)
timer.timeout.connect(self.update)
timer.start(1000)
quitAction = QtGui.QAction("E&xit", self)
quitAction.setShortcut("Ctrl+Q")
quitAction.triggered.connect(app.quit)
self.addAction(quitAction)
self.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu)
self.setToolTip(
"Drag the clock with the left mouse button.\n"
+ "Use the right mouse button to open a context menu.")
self.setWindowTitle("Shaped Analog Clock")
self.drag_position = QtCore.QPoint()
def mouseMoveEvent(self, event):
if event.buttons() & QtCore.Qt.LeftButton:
event_point = event.globalPosition().toPoint()
self.move(event_point - self.drag_position)
event.accept()
def mousePressEvent(self, event):
if event.button() == QtCore.Qt.LeftButton:
event_point = event.globalPosition().toPoint()
top_left = self.frameGeometry().topLeft()
self.drag_position = event_point - top_left
event.accept()
def paintEvent(self, event):
hour_hand = [
QtCore.QPoint(7, 8),
QtCore.QPoint(-7, 8),
QtCore.QPoint(0, -40),
]
minute_hand = [
QtCore.QPoint(7, 8),
QtCore.QPoint(-7, 8),
QtCore.QPoint(0, -70),
]
hour_color = QtGui.QColor(127, 0, 127)
minute_color = QtGui.QColor(0, 127, 127, 191)
side = min(self.width(), self.height())
time = QtCore.QTime.currentTime()
painter = QtGui.QPainter(self)
painter.setRenderHint(painter.Antialiasing)
painter.translate(self.width() / 2, self.height() / 2)
painter.scale(side / 200, side / 200)
painter.setPen(QtCore.Qt.NoPen)
painter.setBrush(self.palette().window())
painter.drawEllipse(QtCore.QPoint(0, 0), 98, 98)
painter.setPen(QtCore.Qt.NoPen)
painter.setBrush(hour_color)
painter.save()
painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)))
painter.drawConvexPolygon(hour_hand)
painter.restore()
painter.setPen(hour_color)
for i in range(12):
painter.drawLine(88, 0, 96, 0)
painter.rotate(30.0)
painter.setPen(QtCore.Qt.NoPen)
painter.setBrush(minute_color)
painter.save()
painter.rotate(6.0 * (time.minute() + time.second() / 60.0))
painter.drawConvexPolygon(minute_hand)
painter.restore()
painter.setPen(minute_color)
for j in range(60 // 5):
painter.drawLine(92, 0, 96, 0)
painter.rotate(6.0 * 5)
def __resizeEvent(self, event):
side = min(self.width(), self.height())
masked_region = QtCore.QRegion(
self.width() / 2 - side / 2,
self.height() / 2 - side / 2,
side, side,
QRegion.Ellipse,
)
self.setMask(maskedRegion)
clock = ShapedClock()
clock.show()
app.exec_()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment