Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6853257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:29:26+00:00 2026-05-27T01:29:26+00:00

I have some working code that I altered but I can’t seem to get

  • 0

I have some working code that I altered but I can’t seem to get the math right for the hours variable. I think I had to much food for the holidays because I’m coming up with a blank.

## {{{ http://code.activestate.com/recipes/124894/ (r2)
from Tkinter import *
import time
import pygtk
import gtk
import time

class StopWatch(Frame):  
    """ Implements a stop watch frame widget. """                                                                
    def __init__(self, parent=None, **kw):        
        Frame.__init__(self, parent, kw)
        self._start = 0.0        
        self._elapsedtime = 0.0
        self._running = 0
        self.timestr = StringVar()               
        self.makeWidgets()      

    def makeWidgets(self):                         
        """ Make the time label. """
        l = Label(self, textvariable=self.timestr)
        self._setTime(self._elapsedtime)
        l.pack(fill=X, expand=NO, pady=2, padx=2)                      

    def _update(self): 
        """ Update the label with elapsed time. """
        self._elapsedtime = time.time() - self._start
        self._setTime(self._elapsedtime)
        self._timer = self.after(50, self._update)

    def _setTime(self, elap):
        """ Set the time string to Minutes:Seconds:Hundreths """
        hours = int(elap) #cant remember formula 
        minutes = int(elap/60)
        seconds = int(elap - minutes*60.0)
        hseconds = int((elap - minutes*60.0 - seconds)*100)
        sn = time.strftime('%m/%d/%Y-%H:%M:%S')                
        self.timestr.set('%02s\n\n%02dh:%02dm:%02ds:%02d' % (sn,hours,minutes, seconds, hseconds))

    def Start(self):                                                     
        """ Start the stopwatch, ignore if running. """
        if not self._running:            
            self._start = time.time() - self._elapsedtime
            self._update()
            self._running = 1        

    def Stop(self):                                    
        """ Stop the stopwatch, ignore if stopped. """
        if self._running:
            self.after_cancel(self._timer)            
            self._elapsedtime = time.time() - self._start    
            self._setTime(self._elapsedtime)
            self._running = 0

    def Reset(self):                                  
        """ Reset the stopwatch. """
        self._start = time.time()         
        self._elapsedtime = 0.0    
        self._setTime(self._elapsedtime)



def main():
    root = Tk()
    root.title( "Stop Watch" )
    sw = StopWatch(root)
    sw.pack(side=TOP)

    Button(root, text='Start', command=sw.Start).pack(side=LEFT)
    Button(root, text='Stop', command=sw.Stop).pack(side=LEFT)
    Button(root, text='Reset', command=sw.Reset).pack(side=LEFT)
    Button(root, text='Quit', command=root.quit).pack(side=LEFT)

    root.mainloop()

if __name__ == '__main__':
    main()
## end of http://code.activestate.com/recipes/124894/ }}}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:29:27+00:00Added an answer on May 27, 2026 at 1:29 am

    I infer from the equations that elap is measured in seconds. Since you are extracting hours into a separate variable, you need to remove them from the minutes tally. And of course you need to follow this through the rest of the calculation since the meaning of minutes is changed from the original code.

    hours = int(elap/3600)
    minutes = int((elap-hours*3600)/60)
    seconds = int(elap-hours*3600-minutes*60)
    hseconds = int((elap-hours*3600-minutes*60-seconds)*100)
    

    I think if I was writing this I would modify elap as I was going along to reduce the duplication.

    hours = int(elap/3600)
    elap -= hours*3600
    minutes = int(elap/60)
    elap -= minutes*60
    seconds = int(elap)
    elap -= seconds
    hseconds = int(elap*100)
    

    Doing it this way makes it far easier to see what is going on and also easier to modify in future. For example, if you wanted to add days into the mix then all you need to do is graft this onto the beginning of the code:

    days = int(elap/86400)
    elap -= days*86400
    

    Now, I’ve written the code here assuming that elap is a float, which of course it is in your program. If you were particularly paranoid you would write elap = float(elap) before performing the arithmetic.

    But I agree with @soulcheck that it’s much cleaner to use a library function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some working code that uses IImgCtx to load images, but I can't
I have been working on some legacy C++ code that uses variable length structures
I have some terribly written ASP.NET code that is just not working right (go
I have some heavily templated c++ code that I am working with. I can
I have some code (that is working), but I just want to make sure
I have some code I am working on that worked just fine until I
I have some really complicated legacy code I've been working on that crashes when
I have to modify some code in a application I am working on that
So far, I have some working code that goes into a list of servers
I have some code that I would swear was working a month ago. However,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.