Python Timer Code- using Methods

Timer is a very basic application you have used in all modern gadgets. It can be used to monitor duration of an event. A user can start timer and stop it. Lets understand the Python Timer Code created with graphics functions available in module.

Graphics Functions used in Python Timer Code

setBackground– This method sets the background color of the window object of GraphWin. It is called with a GraphWin object by passing a color in string or RGB value as parameter.

setTextColor– This method sets the color of a text object on a GraphWin window. It is called with a Text object and a color in string or RGB value is passed.

setSize– this method is used to set the text size of a Text object. An integer value of maximum value 36 can be passed.

setStyle– this method is used to set the text style of a Text object. The values allowed are strings ‘bold’, ‘normal’, ‘italic’ and ‘bold italic’.

setFace– this method is used to set the font face of a Text object. The values allowed are strings ‘helvetica’, ‘arial’, ‘courier’ or ‘times roman’.

undraw– this method clears the specified graphics object. It is called with any declared and initialized graphics object. In the Python Timer Code this method is called with the text that stores the minutes and seconds of the timer to clear it before drawing the next timer value after incrementing in while loop.

checkMouse– This method is called with a declared GraphWin object. It returns the last key typed or mouse click. In the Python Timer Code this method is checked in the loop used to increment  and display time. The loop is stopped with break statement. The timer window is closed if user has pressed a key or clicked mouse.

getMouse– This method listens to  the mouse for a click action. It returns X and Y coordinate values of window where the mouse has been clicked.  It is called with a GraphWin object without any parameters.  In our Python Timer Code this function is used for associating timer start with mouse click.

Time Module Method used in Code

Sleep– this method is defined in the time module and is used to delay program execution by number of seconds passed as parameter. In the example we have passed one second as delay to increment seconds. When value of seconds part reaches 60 minutes part value is incremented by 1 after resetting seconds to 1.


# A simple graphical Timer Application in Python
import time
from graphics import *

def main():
    workArea = GraphWin('A Simple Python timer', 300, 300) # give title and dimensions
    message = Text(Point(workArea.getWidth()/2-len('Click to start Timer')/2, 50), 'Click to start Timer')
    message.setFace('helvetica')#change text style
    message.setTextColor("blue") #change text color
    workArea.getMouse()# get mouse click on screen to start timer
    start = True

    # initilize variables
    vSec = 0
    vMin = 0
    timerCont = start
    while timerCont:
        vSec += 1
        label=Text(Point(workArea.getWidth()/2-len("Minutes:Seconds")/2, 100), "Minutes:Seconds")
        label.setTextColor("green") #change color
        message= Text(Point(workArea.getWidth()/2, 150), str(vMin)+ " : " + str(vSec) )
        message.setTextColor("purple") #change color
        if vSec == 60:
            vSec = 0
            vMin += 1
        if workArea.checkMouse():

Before starting timer

python Timer Code- before start

Timer Ticking

Ticking Timer

This Python Timer Code is a simple implementation. You can copy it and modify to match your need. Do share updated code with your personal touch to enhance its looks or working. We will be looking forward to your code in comments section!!

Be First to Comment

Leave a Reply

Your email address will not be published.