• 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()

  • 相关阅读:
    实战 | 服务端开发与计算机网络结合的完美案例
    企业微信获取客户群里用户的unionid;企业微信获取客户详情
    使用 gperftools 分析程序内存占用情况
    【前端】JavaScript-PC端网页特效
    (附源码)app校园购物网站 毕业设计 041037
    C语言程序设计 复习总结[持续更新ing]
    机器学习与数据挖掘第三、四周
    基于springboot人事管理系统java项目介绍
    webrtc一对一视频通话功能实现
    springboot测试类,注解
  • 原文地址:https://blog.csdn.net/qq_19309473/article/details/134365223