2011-11-24 128 views
3

我有一些工作代碼,我改變了,但我似乎無法得到小時變量的數學權利。我想我得爲節日準備很多食物,因爲我想出了一個空白。蟒蛇和小時公式計算

## {{{ 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/ }}} 
+0

你肯定分鐘= ELAP/60? – Kos

+0

這部分包含在代碼中,我只是想添加一個小時變量。我知道它看起來很奇怪,但它似乎工作.... –

+0

它是elap/60。 time()返回浮點數,表示秒數開始後的秒數,因此1.0是1秒3600.0是3600秒等。 – soulcheck

回答

2

我從等式推導出elap是以秒爲單位測量的。由於您正在將小時提取到單獨的變量中,因此您需要將其從分鐘計數中刪除。當然,由於minutes的含義已從原始代碼改變,因此您需要在計算的其餘部分中進行跟蹤。

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) 

我想,如果我在寫這一點,我會修改elap我打算一起,以減少重複。

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

這樣做使得它更容易看到發生了什麼,也更容易在將來修改。例如,如果你想天添加到組合,那麼所有你需要做的就是這個移植到代碼的開頭:

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

現在,我已經寫在這裏假設elapfloat代碼,這當然是在你的程序中。如果你特別偏執,你會在執行算術之前編寫elap = float(elap)

但我同意@soulcheck使用庫函數更簡潔。

+0

你的代碼工作正常,但我認爲它可以做得更優雅。看到我的答案。 –

+0

謝謝!這真的有幫助 –

0
hours = int(elap/ 3600) 

minutes = int((elap % 3600)/60) 

seconds = int(elap % 60) 

hseconds = int((elap % 1) * 100) 

也許你會更好地將它與datetime.fromtimestamp轉換並使用它。

編輯:添加了所有丟失的公式

+0

這是正確的,但它仍然有待於秒和時間 –

+1

我認爲裏克T會注意到的模式;) – soulcheck

+0

是的,謝謝大家!我走到我的第二個感恩節盛宴......感謝上帝爲運動褲;-) –

0

當時無法進行測試,而是通過代碼elap會是傳遞秒的量。因此,你將它除以3600,並用int()將其舍入。對於前面的代碼來說,這意味着它可能有90分鐘的時間,但現在應該是1小時30分鐘。因此除了計算小時數之外,您還必須相應地調整minutessecondshseconds

def _setTime(self, elap): 
    """ Set the time string to Hours:Minutes:Seconds:Hundreths """ 
    hours = int(elap/3600) 
    minutes = int(elap/60 - hours*60.0) 
    seconds = int(elap - hours*3600.0 - minutes*60.0) 
    hseconds = int((elap - hours*3600.0 - 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)) 
+0

你的代碼工作正常,但我認爲它可以變得更優雅。 –

5

從另一端開始工作要容易得多。你不需要像86400這樣的大數字,這使得代碼審查人員可以使用他們的計算器應用程序。

c = int(elap * 100) # centiseconds 
s, c = divmod(c, 100) 
m, s = divmod(s, 60) 
h, m = divmod(m, 60) 
d, h = divmod(h, 24) 
print(d, h, m, s, c) 

,或者避免函數調用:

c = int(elap * 100) # centiseconds 
s = c // 100; c %= 100 
m = s // 60; s %= 60 
h = m // 60; m %= 60 
d = h // 24; h %= 24 
print(d, h, m, s, c) 
+1

+1這很好,讀得很好。現在我們只需要等待其他人前來,並用另一個「你的代碼正常工作......」的評論來打敗你。 ;-) –

+0

haha​​hhaah更好的答案比我說少;-) ...感謝您的幫助,這真的幫助傢伙.... –