Prerequisite: Introduction to pyqt-5
A clock or watch is called “analog” when it has moving hands and (usually) hours marked from number 1 to 12 to show you the time. Some have Roman Numerals (I, II, III, etc) instead, or no numbers at all! In other words: not a digital clock.
PyQt5 is a cross-platform GUI toolkit, a set of python bindings for Qt v5. One can develop an interactive desktop application with so much ease because of the tools and simplicity provided by this library. A GUI application consists of Front-end and Back-end.
Approach :
- Create a Clock class which inherits the QMainWindow class.
- Inside the Clock class creating a timer object which updates the whole code after each second.
- Create three polygon objects for each hand of the clock .
- Create a paint event method.
- Inside the paint event method get the current time and the minimum of windows width or height .
- Create a painter object and a method to draw the hands.
- Inside the method for drawing hands(pointer) take arguments like color, rotation and polygon object.
- Rotate the painter object and draw the pointer.
- Inside the paint event method, according to the current time set the rotation value and call the draw pointer method.
- Draw the background image of the clock i.e lines for each hour.
Below is the implementation :
Python3
# importing libraries from PyQt5.QtWidgets import * from PyQt5 import QtCore, QtGui from PyQt5.QtGui import * from PyQt5.QtCore import * import sys # creating a clock class class Clock(QMainWindow): # constructor def __init__( self ): super ().__init__() # creating a timer object timer = QTimer( self ) # adding action to the timer # update the whole code timer.timeout.connect( self .update) # setting start time of timer i.e 1 second timer.start( 1000 ) # setting window title self .setWindowTitle( 'Clock' ) # setting window geometry self .setGeometry( 200 , 200 , 300 , 300 ) # setting background color to the window self .setStyleSheet( "background : black;" ) # creating hour hand self .hPointer = QtGui.QPolygon([QPoint( 6 , 7 ), QPoint( - 6 , 7 ), QPoint( 0 , - 50 )]) # creating minute hand self .mPointer = QPolygon([QPoint( 6 , 7 ), QPoint( - 6 , 7 ), QPoint( 0 , - 70 )]) # creating second hand self .sPointer = QPolygon([QPoint( 1 , 1 ), QPoint( - 1 , 1 ), QPoint( 0 , - 90 )]) # colors # color for minute and hour hand self .bColor = Qt.green # color for second hand self .sColor = Qt.red # method for paint event def paintEvent( self , event): # getting minimum of width and height # so that clock remain square rec = min ( self .width(), self .height()) # getting current time tik = QTime.currentTime() # creating a painter object painter = QPainter( self ) # method to draw the hands # argument : color rotation and which hand should be pointed def drawPointer(color, rotation, pointer): # setting brush painter.setBrush(QBrush(color)) # saving painter painter.save() # rotating painter painter.rotate(rotation) # draw the polygon i.e hand painter.drawConvexPolygon(pointer) # restore the painter painter.restore() # tune up painter painter.setRenderHint(QPainter.Antialiasing) # translating the painter painter.translate( self .width() / 2 , self .height() / 2 ) # scale the painter painter.scale(rec / 200 , rec / 200 ) # set current pen as no pen painter.setPen(QtCore.Qt.NoPen) # draw each hand drawPointer( self .bColor, ( 30 * (tik.hour() + tik.minute() / 60 )), self .hPointer) drawPointer( self .bColor, ( 6 * (tik.minute() + tik.second() / 60 )), self .mPointer) drawPointer( self .sColor, ( 6 * tik.second()), self .sPointer) # drawing background painter.setPen(QPen( self .bColor)) # for loop for i in range ( 0 , 60 ): # drawing background lines if (i % 5 ) = = 0 : painter.drawLine( 87 , 0 , 97 , 0 ) # rotating the painter painter.rotate( 6 ) # ending the painter painter.end() # Driver code if __name__ = = '__main__' : app = QApplication(sys.argv) # creating a clock object win = Clock() # show win.show() exit(app.exec_()) |
Output: