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()
相关推荐
Xeon_CC4 分钟前
用C++编写屏幕准星
开发语言·c++
孤存5229 分钟前
c语言动态内存管理
c语言·开发语言·算法
Ali8852011 分钟前
Python字符串方法速查表大全
前端·python
开源量化GO14 分钟前
近期量化实现:API 数据、策略逻辑和交易执行要连成一条线
人工智能·python
小柯南敲键盘19 分钟前
跨马翻译:跨境电商批量图片翻译与视频字幕工具推荐
人工智能·python·音视频
冰暮流星22 分钟前
javascript之webstorage用法
开发语言·javascript·ecmascript
Haibakeji23 分钟前
APP小程序定制开发项目频繁延期?前期规避方法与落地避坑指南
小程序·uni-app·软件需求
我一定会有钱25 分钟前
打造完美工作流:Git 配置”一次推送,三端同步”(Gitee/GitCode/GitHub)
开发语言·windows·vscode·搜索引擎·gitlab·github·visual studio code
matlab代码40 分钟前
基于matlab肤色人脸标记检测系统 GUI界面【源码53期】
开发语言·matlab·人脸标记
李可以量化1 小时前
Redis 从了解到精通(一・上):量化开发必学的三大基础数据类型
python