python实现一个简介桌面倒计时小程序

本章内容主要是利用python制作一个简单的桌面倒计时程序,包含开始、重置 、设置功能。

目录

一、效果演示

二、程序代码


一、效果演示

二、程序代码

python 复制代码
#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
@author: Roc-xb
"""

import tkinter as tk
from tkinter import simpledialog
from tkinter import messagebox

class CountdownTimer:
    def __init__(self, root):
        self.root = root
        self.root.title("倒计时程序")
        self.root.geometry("450x300")

        self.countdown_value = 60
        self.is_counting = False

        self.canvas = tk.Canvas(self.root, width=200, height=200, bg="white")
        self.canvas.place(x=20, y=20)

        self.countdown_label = tk.Label(self.root, text="倒计时: 60s", font=("Arial", 20))
        self.countdown_label.place(x=250, y=20)

        self.start_button = tk.Button(self.root, text="开始", command=self.start_countdown)
        self.start_button.place(x=250, y=70)

        self.reset_button = tk.Button(self.root, text="重置", command=self.reset_countdown)
        self.reset_button.place(x=250, y=120)

        self.set_button = tk.Button(self.root, text="设置", command=self.set_countdown)
        self.set_button.place(x=250, y=170)

    def start_countdown(self):
        if self.is_counting:
            return

        self.is_counting = True
        self.countdown()

    def countdown(self):
        if self.countdown_value > 0 and self.is_counting is True:
            self.countdown_value -= 1
            self.countdown_label.config(text="倒计时: " + str(self.countdown_value) + "s")
            self.canvas.delete("all")
            self.canvas.create_rectangle(0, 200 - self.countdown_value * 2, 200, 300, fill="green")
            self.root.after(1000, self.countdown)
        elif self.countdown_value > 0 and self.is_counting is False:
            self.canvas.delete("all")
            self.is_counting = False
            return
        else:
            self.is_counting = False
            messagebox.showinfo("提示", "倒计时结束")

    def reset_countdown(self):
        self.is_counting = False
        self.countdown_value = 60
        self.countdown_label.config(text="倒计时: " + str(self.countdown_value) + "s")
        self.canvas.delete("all")

    def set_countdown(self):
        if self.is_counting:
            return

        value = tk.simpledialog.askinteger("设置倒计时", "请输入倒计时时间(秒):", parent=self.root)
        if value is not None:
            self.countdown_value = value
            self.countdown_label.config(text="倒计时: " + str(self.countdown_value) + "s")
            self.canvas.delete("all")


if __name__ == '__main__':
    root = tk.Tk()
    app = CountdownTimer(root)
    root.mainloop()
相关推荐
装不满的克莱因瓶7 分钟前
了解 LangChain 中的 LLM 与 ChatModel 的差异
人工智能·python·ai·langchain·llm·agent·chatmodel
宋拾壹1 小时前
同时添加多个类目
android·开发语言·javascript
IT知识分享1 小时前
从零开发在线简繁转换工具:OpenCC 实战、避坑经验与方案选型
javascript·python
lunzi_08261 小时前
【学习笔记】《Python编程 从入门到实践》第8章:函数定义、参数传递与模块导入
笔记·python·学习
凡人叶枫1 小时前
Effective C++ 条款04:确定对象被使用前已先被初始化
java·linux·开发语言·c++·嵌入式开发
杨运交1 小时前
[030][Web模块]Spring Boot 验证与 OpenAPI 集成实战:从校验规则到文档生成
前端·spring boot·python
克里斯蒂亚诺更新2 小时前
微信小程序使用vant4 weapp自定义菜单 但是弹出层却被菜单遮挡的解决办法
微信小程序·小程序·notepad++
培培说证2 小时前
2026财务岗位如何快速提升自身能力
python
小小龙学IT2 小时前
Go 语言后端开发:从并发模型到生产落地的工程实践
开发语言·后端·golang
努力攻坚操作系统2 小时前
编程语言编译运行机制对比:C / Java / Python
java·c语言·python