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()
相关推荐
zyx没烦恼9 小时前
Qt 基础编程核心知识点全解析:含 Hello World 实现、对象树、坐标系及开发工具使用
开发语言·qt
木心爱编程9 小时前
C++链表实战:STL与手动实现详解
开发语言·c++·链表
mkhase9 小时前
9.11-QT-QT的基本使用
开发语言·qt
Kyln.Wu10 小时前
【python实用小脚本-211】[硬件互联] 桌面壁纸×Python梦幻联动|用10行代码实现“开机盲盒”自动化改造实录(建议收藏)
开发语言·python·自动化
杨天天.10 小时前
小程序原生实现音频播放器,下一首上一首切换,拖动进度条等功能
前端·javascript·小程序·音视频
weixin_1772972206910 小时前
盲盒抽卡机小程序:从0到1的蜕变之路
小程序·盲盒·抽谷机
Ms_Big10 小时前
ppliteseg改rknn,部署在嵌入式板,加速模型
人工智能·python·深度学习
说私域10 小时前
基于开源AI智能名片链动2+1模式S2B2C商城小程序的移动互联网人气氛围营造机制研究
人工智能·小程序·开源
稻草人想看远方10 小时前
GC垃圾回收
java·开发语言·jvm
胡萝卜的兔11 小时前
go 日志的分装和使用 Zap + lumberjack
开发语言·后端·golang