gpt4 book ai didi

python - 将秒转换为分秒

转载 作者:太空狗 更新时间:2023-10-29 21:19:48 29 4
gpt4 key购买 nike

我已经在 python 2.7 中为计时器程序编写了一些代码。缺点是结果以秒为单位,而我需要在 Minutes:Seconds 内得到结果。我试图格式化时间无济于事。谁能帮忙?”

from Tkinter import *
import time

root = Tk()
root.title("Timer")
canvas = Canvas(root)
canvas.pack()

time=1

def tick():

canvas.delete(ALL)
global time
time += 10


if time >= 120:

canvas.create_text(180,140, font = ("Calibri",200),text=time,fill='red')
else:

canvas.create_text(180,140, font = ("Calibri",200),text=time,fill='green')
if time == 1000:
time = 0
tick()
else:
canvas.after(1000, tick)

def reset():
global time
time = 0

def close_window():
root.destroy()

b = Button(root,text="Reset", command=reset)
c = Button(root,text="Quit", command=close_window)
b.pack(side = LEFT)
c.pack(side = RIGHT)
canvas.after(1, tick)
root.mainloop()

最佳答案

您可以使用datetime.timedelta

import datetime
number_of_seconds = 1000
str(datetime.timedelta(seconds=number_of_seconds))

# output
'0:16:40'

或者更好,因为您只需要 mm:ss,您可以使用 time.strftime

import time
number_of_seconds = 1000
time.strftime("%M:%S", time.gmtime(number_of_seconds))

# output
'16:40'

文档可用for datetime modulefor time module .

关于python - 将秒转换为分秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27751274/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com