如何将倒计时更改为小时/分钟/秒 [英] How to change countdown to hour/minutes/seconds

查看:53
本文介绍了如何将倒计时更改为小时/分钟/秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Tkinter 的新手,所以我只是想尽可能多地学习.我想尝试制作闹钟,但现在我坚持时间格式.这是当前代码:

I am new to Tkinter so im just trying to learn as much as possible. I want to try and make an alarm clock but now im stuck on the time format. This is the current code:

from tkinter import *

teer = Tk()
field = Canvas(teer, bd=0, highlightthickness=0, height='190', width='400', bg='#111111')
field.pack()

def start_countdown(count):
    coin = 0.5
    teer.resizable(False,False)
    counter = Label(teer, fg = "#287aff", bg='#232323', font = ("Lato", 35, "bold"), width='15')
    counter.place(x=50, y=50)
    counter["text"] = count
    if count > 0:
       teer.after(1000, start_countdown, count -1)
    if count < 500:
       coin = 0.6
    if count < 300:
       coin = 0.7

start_countdown(500)
teer.mainloop()

现在我一直在尝试将 500(秒)缩短为分钟/秒.或者如果我可以选择在函数中插入一个大于 3600 的整数,最终将其更改为小时/分钟/秒.我只想要硬编码的时间,所以我认为这不会是一个问题.

now what i've been trying to do is chop the 500 (seconds) up into minutes / seconds. Or ultimately change it to hours / minutes / seconds if i may choose to insert an int larger than 3600 into the function. I just want the time hardcoded so i thought it wouldn't be such a problem.

我尝试了什么:

-试验了人们制作的不同警报/倒计时(遗憾的是,倒计时而不是倒计时的并不多,而且也是以小时/分钟/秒为单位.

-Experimented with different alarms / countdowns that people made (sadly there aren't many out there that count down instead of up and are also in hours/minutes/seconds.

-实验了格式(例如)%H:%M:%S

-Experimented with the format (for example) %H:%M:%S

我只是似乎不明白.非常感谢有关制作倒计时的 GUI 程序的任何帮助或建议.

I Just don't seem to get it. Would appreciate any help or advice about making a GUI-program that counts down.

推荐答案

可以使用divmod来计算剩余时间.

You can use divmod to calculate the remaining time.

import tkinter as tk
root = tk.Tk()

a = tk.Label(root,text="")
a.pack()

def set_idle_timer(t):
    hours, remainder = divmod(t, 3600)
    mins, secs = divmod(remainder, 60)
    timeformat = "{:02d}:{:02d}:{:02d}".format(hours, mins, secs)
    a.config(text=timeformat)
    t -=1
    root.after(1000,lambda: set_idle_timer(t))

set_idle_timer(3605)

root.mainloop()

这篇关于如何将倒计时更改为小时/分钟/秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆