当前位置:   article > 正文

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

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号