To start with, we define the AnalogClock
class that is inherited from QWidget
. We define two variables that will be used to draw the hourHand
and minuteHand
of the analog clock. We also define the colors for the pointers:
class AnalogClock(QWidget): hourHand = QPolygon([ QPoint(7, 8), QPoint(-7, 8), QPoint(0, -40) ]) minuteHand = QPolygon([ QPoint(7, 8), QPoint(-7, 8), QPoint(0, -70) ]) hourColor = QColor(255, 0, 127) minuteColor = QColor(0, 127, 127, 255)
Next, we define an init function that will start the timer that will update the clock on the expiry of every minute. It also resizes the widget and sets a title for it:
def __init__(self, parent=None): QWidget.__init__(self) timer = QTimer(self) timer.timeout.connect(self.update) timer.start(1000) self.setWindowTitle("Analog Clock") self.resize(200, 200)
The core functionality of the analog clock is defined...