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

news/2024/7/20 0:48:07 标签: 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()


http://www.niftyadmin.cn/n/5175197.html

相关文章

DAY52 300.最长递增子序列

300.最长递增子序列 题目要求:给你一个整数数组 nums ,找到其中最长严格递增子序列的长度。 子序列是由数组派生而来的序列,删除(或不删除)数组中的元素而不改变其余元素的顺序。例如,[3,6,2,7] 是数组 […

算法通关村第七关-青铜挑战二叉树的深度优先遍历(递归)

二叉树的深度优先遍历 今天我们来说二叉树的深度优先遍历 , 这次用简单但有点难理解的方式递归来实现 , 对应LeetCode 144,145 二叉树的前序遍历 描述 : 给你二叉树的根节点 root ,返回它节点值的 前序 遍历。 题目 : LeetCode 二叉树的前序遍历 : 144. 二叉…

Springboot通过ObjectMapper(节点树)解析JSON

1、ObjectMapper通过节点树的方式解析JSON字符串 1.1、通过节点直接获取属性值 1.1.1、测试代码 node.get("order_id"):直接获取JSON中属性对应的值 Test public void parseJson() throws Exception{//创建json字符串,模拟从外界接收的订…

ubuntu下tensorrt环境配置

文章目录 一、Ubuntu18.04环境配置1.1 安装工具链和opencv1.2 安装Nvidia相关库1.2.1 安装Nvidia显卡驱动1.2.2 安装 cuda11.31.2.3 安装 cudnn8.21.2.4 下载 tensorrt8.4.2.4 二、编写CMakeLists.txt三、TensorRT系列教程 一、Ubuntu18.04环境配置 教程同样适用与ubuntu22.04…

Pandas教程(非常详细)(第五部分)

接着Pandas教程(非常详细)(第四部分),继续讲述。 二十五、Pandas sample随机抽样 随机抽样,是统计学中常用的一种方法,它可以帮助我们从大量的数据中快速地构建出一组数据分析模型。在 Pandas…

井底的蜗牛

描述 井底的蜗牛距离井口 m 米。白天蜗牛向上爬若干米,晚上蜗牛滑下来 1 米。每天白天向上爬的距离是昨天的1/3。已知第一天白天能向上爬 n 米,问几天后蜗牛能爬出井外(到井口就出去了)。 输入 输入数据是两个实数,m和n,分别是…

mini-vue 的设计

mini-vue 的设计 mini-vue 使用流程与结果预览&#xff1a; <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8" /><meta http-equiv"X-UA-Compatible" content"IEedge" /><meta name&qu…

瑞吉外卖Day03

小张推荐:瑞吉外卖Day02 1.启用/禁用员工账号 1.1 思路分析 1.2Controller层 PutMapping()public R<String> update(RequestBody Employee employee, HttpServletRequest request) {log.info(employee.toString());Long emp (Long) request.getSession().getAttribu…