hi guys.. i implemented a stopwatch and as i run my software, the stopwatch starts. what i want is to implement a method to start the stopwatch and another method to stop the stopwatch.

can u give me an example of this please? thanks..

7 Years
Discussion Span
Last Post by harrykokil
class StopWatch(object):
    def start(self):
        #code that starts

    def stop(self):
        #code that stops

Can't give any more help than that without a better description of your problem.


Using a GUI would allow you to use buttons to start and stop your stopwatch. Here is a very basic Tkinter GUI example ...

# a very simple stopwatch using Tkinter
# with Python30 replace Tkinter with tkinter

import Tkinter as tk
import time

def start():
    global count_flag
    count_flag = True
    count = 0.0
    while True:
        if count_flag == False:
        # put the count value into the label
        label['text'] = str(count)
        # wait for 0.1 seconds
        # needed with time.sleep()
        # increase count
        count += 0.1

def stop():
    global count_flag
    count_flag = False

# create a Tkinter window
root = tk.Tk()

# this will be a global flag
count_flag = True

# create needed widgets
label = tk.Label(root, text='0.0')
btn_start = tk.Button(root, text='start', command=start)
btn_stop = tk.Button(root, text='stop', command=stop)

# use a grid to place the widgets
label.grid(row=0, column=0, columnspan=2)
btn_start.grid(row=1, column=0, padx=5, pady=5)
btn_stop.grid(row=1, column=1, padx=5)

# start the event loop

If you are not familiar with GUI programming, now is a good time to learn.

Attachments tk_stopwatch.jpg 3.76 KB
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.