带有 GUI 的 Python 倒计时时钟 [英] Python countdown clock with GUI

查看:46
本文介绍了带有 GUI 的 Python 倒计时时钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Python 中为 Raspberry Pi 制作的倒计时时钟出现问题.我需要一个从 60 分钟开始倒计时的倒计时时钟.当时间用完时,它应该显示一个红色的文字游戏结束".

I'm having problems with a countdown clock that I was making in Python for a Raspberry Pi. I need to have a countdown clock that counts down from 60 minutes. When time runs out it should display a red text "GAME OVER".

我已经使用 TKinter 和一个用于实际计时器的 for 循环制作了一个,但我找不到任何方法来停止 for 循环.我放弃了.

I've already made one using TKinter and a for loop for the actual timer but I couldn't find any way to stop the for loop. I gave up on it.

有没有足够好的人可以编写实际的计时器和计时器停止部分?我在 python 和 TKinter 方面足够好,可以做我需要的所有其他事情.

Is there anyone nice enough to maybe write the actual timer and timer stopping part? I'm good enough at python and TKinter to do everything else that I need.

推荐答案

我建议使用 generators 来处理您的 for 循环,并将提供一个最小的示例,但在 StackOverflow 上没有人会编写实际计时器和计时器停止部分"(请参阅​​我可以在这里问什么主题)

I'd recommend using generators to handle your for loop and will provide a minimal example but on StackOverflow no one is going to "write the actual timer and timer stopping part" (see What topics can I ask here)

请注意,这是我在发布此问题之前的一个示例,我认为它会对您有所帮助.

Note this is an example I had before this question was posted and thought it would be helpful to you.

import tkinter as tk

def run_timer():
    for time in range(60):
        label["text"] = time #update GUI here
        yield #wait until next() is called on generator

root = tk.Tk()

label = tk.Label()
label.grid()

gen = run_timer() #start generator
def update_timer():
    try:
        next(gen)
    except StopIteration:
        pass #don't call root.after since the generator is finished
    else:
        root.after(1000,update_timer) #1000 ms, 1 second so it actually does a minute instead of an hour

update_timer() #update first time

root.mainloop()

您仍然需要自己弄清楚如何实现 after_cancel() 来阻止它和红色的GAME OVER"文本.

you will still need to figure out for yourself how to implement after_cancel() to stop it and the red "GAME OVER" text.

这篇关于带有 GUI 的 Python 倒计时时钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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