1
我在Python中做一個計時器,但是當定時器停止時,我的代碼最終顯示兩個對話框而不是一個。我能做些什麼來避免出現額外的窗口?tkMessageBox在定時器程序中有額外的窗口
代碼timer.py
如下:
#!/usr/bin/python
import time
start = time.time()
interval=1
import sys
import tkMessageBox
def showmessage():
tkMessageBox.showinfo("timer.py", "Timer Complete!")
if __name__ == "__main__":
if len(sys.argv) > 1:
ns = {'__builtins__': None}
seconds=int(eval(str(sys.argv[1]), ns))
if len(sys.argv) > 2:
if sys.argv[2]=="mins" or sys.argv[2]=="min":
seconds=seconds*60
interval = 1 if seconds < interval else interval
second=seconds - (time.time() - start) + 0.5
while second >= 0:
#In Python 3: print('string', end='\r')
if int(second/3600) > 0:
print "\rTime =%3d hrs %02d mins %02d sec" % \
(second/3600, (second % 3600)/60, second % 60),
else:
print "\rTime =%3d mins %02d sec" % \
(second/60, second % 60),
sys.stdout.flush()
time.sleep(interval)
if int(second % 60+0.5) == 0:
print #or sys.stdout.write("\n")
second=seconds - (time.time() - start) + 0.5
showmessage()
else:
print ("Usage: %s [seconds]" % str(sys.argv[0]))
print (" %s [minutes] mins" % str(sys.argv[0]))