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()
相关推荐
吴声子夜歌13 小时前
Java扩展——OkHttp
java·开发语言·okhttp
hhzz13 小时前
【OpenCV 入门到精通 01】认识 OpenCV 与计算机视觉:从零建立全局认知
人工智能·python·opencv·计算机视觉·开源
啊阿狸不会拉杆13 小时前
《计算机网络-自顶向下方法》5.7 网络管理、SNMP和NETCONF/YANG 读书笔记
开发语言·计算机网络·php
长友cy13 小时前
Qt官方示例D-BusListNames阅读
开发语言
萧瑟余晖13 小时前
Java深入解析篇六十一之编译器API(javax.tools)详解
java·开发语言
蜡台13 小时前
Kotlin 零基础完整版实战教程|从语法入门到Android工程实战
android·开发语言·kotlin
jimy113 小时前
C++ 的 “移动语义”——Move Semantics
开发语言·c++
码云数智-园园13 小时前
C++ STL 容器怎么选?vector、list、map、unordered_map 优缺点对比
java·开发语言
一马平川的大草原13 小时前
如何实现SVG转Mermaid图
python·svg·格式转换·mermaid
en.en..13 小时前
竖线 |:管道、按位或、掩码组合
java·开发语言