from Tkinter import * import time class StopWatch(Frame): """ Implements a stop watch frame widget. """ def __init__(self, parent=None, **kw): Frame.__init__(self, parent, kw) self.flag = 0 self._start = 0.0 self._elapsedtime = 0.0 self._running = 0 self.timestr = StringVar() self.makeWidgets() self.control = '' def makeWidgets(self): """ Make the time label. """ l = Label(self, bg = "black", fg = "green", 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/3600) minutes = int(elap/60 - hours*60.0) if minutes % 10 == 0: r = 1 else: r = 0 if r == 0: self.flag = 0 else: # also r == 1 if self.flag == 0: print '\a' # print 'jetzt' self.flag = 1 seconds = int(elap - minutes*60.0 - hours*3600) self.timestr.set('%02d:%02d:%02d' % (hours, minutes, seconds)) self.control = str(hours) + ':' + str(minutes) 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. """ monate = ('Dummy', 'Jan', 'Febr', 'Mrz', 'Apr', 'Mai', 'Juni', 'Juli', 'Aug', 'Sept', 'Okt', 'Nov', 'Dez') now = time.time() datum = time.localtime(now) b = datum[1] f = open('log-' + monate[b]+'.txt' , 'a') f.write(self.control + '\n') f.close() self._start = time.time() self._elapsedtime = 0.0 self._setTime(self._elapsedtime) def main(): root = Tk() root.title('Timer, Version 1.5') 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='Save', command=sw.Reset).pack(side=LEFT) Button(root, text='Quit', command=root.quit).pack(side=LEFT) root.mainloop() if __name__ == '__main__': main()