码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • python实现一个简单的桌面倒计时小程序


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

    目录

    一、效果演示

    二、程序代码


    一、效果演示

    二、程序代码

    1. #!/usr/bin/python
    2. # -*- coding: UTF-8 -*-
    3. """
    4. @author: Roc-xb
    5. """
    6. import tkinter as tk
    7. from tkinter import simpledialog
    8. from tkinter import messagebox
    9. class CountdownTimer:
    10. def __init__(self, root):
    11. self.root = root
    12. self.root.title("倒计时程序")
    13. self.root.geometry("450x300")
    14. self.countdown_value = 60
    15. self.is_counting = False
    16. self.canvas = tk.Canvas(self.root, width=200, height=200, bg="white")
    17. self.canvas.place(x=20, y=20)
    18. self.countdown_label = tk.Label(self.root, text="倒计时: 60s", font=("Arial", 20))
    19. self.countdown_label.place(x=250, y=20)
    20. self.start_button = tk.Button(self.root, text="开始", command=self.start_countdown)
    21. self.start_button.place(x=250, y=70)
    22. self.reset_button = tk.Button(self.root, text="重置", command=self.reset_countdown)
    23. self.reset_button.place(x=250, y=120)
    24. self.set_button = tk.Button(self.root, text="设置", command=self.set_countdown)
    25. self.set_button.place(x=250, y=170)
    26. def start_countdown(self):
    27. if self.is_counting:
    28. return
    29. self.is_counting = True
    30. self.countdown()
    31. def countdown(self):
    32. if self.countdown_value > 0 and self.is_counting is True:
    33. self.countdown_value -= 1
    34. self.countdown_label.config(text="倒计时: " + str(self.countdown_value) + "s")
    35. self.canvas.delete("all")
    36. self.canvas.create_rectangle(0, 200 - self.countdown_value * 2, 200, 300, fill="green")
    37. self.root.after(1000, self.countdown)
    38. elif self.countdown_value > 0 and self.is_counting is False:
    39. self.canvas.delete("all")
    40. self.is_counting = False
    41. return
    42. else:
    43. self.is_counting = False
    44. messagebox.showinfo("提示", "倒计时结束")
    45. def reset_countdown(self):
    46. self.is_counting = False
    47. self.countdown_value = 60
    48. self.countdown_label.config(text="倒计时: " + str(self.countdown_value) + "s")
    49. self.canvas.delete("all")
    50. def set_countdown(self):
    51. if self.is_counting:
    52. return
    53. value = tk.simpledialog.askinteger("设置倒计时", "请输入倒计时时间(秒):", parent=self.root)
    54. if value is not None:
    55. self.countdown_value = value
    56. self.countdown_label.config(text="倒计时: " + str(self.countdown_value) + "s")
    57. self.canvas.delete("all")
    58. if __name__ == '__main__':
    59. root = tk.Tk()
    60. app = CountdownTimer(root)
    61. root.mainloop()

  • 相关阅读:
    Exercise 09
    2021年研究生数学建模D题:抗胰腺癌候选药物的优化建模
    最短路径算法之二:单源有权图,Dijkstra算法,python实现
    正能量早安祝福问候语,早安说说心情短语好句子
    生产制造管理系统对中小型企业的作用有哪些?
    内容平台加码旅游:谁是下一个网红城市
    代码随想录算法训练营第三十一天|贪心算法理论基础、455.分发饼干、376. 摆动序列、53. 最大子序和
    【原创】基于SSM的医院预约挂号系统(医院预约挂号系统毕设源代码)
    【达摩院OpenVI】几行代码,尽享丝滑视频观感
    24 Java xml&&枚举&&注解 详解~
  • 原文地址:https://blog.csdn.net/qq_19309473/article/details/134365223
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号