鱼C论坛

 找回密码
 立即注册
查看: 897|回复: 13

[作品展示] 自制电子钟(tkinter铁粉)

[复制链接]
回帖奖励 10 鱼币 回复本帖可获得 2 鱼币奖励! 每人限 1 次(中奖概率 60%)
发表于 2024-5-15 12:29:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
啥也不说了,上代码
还有待完善

  1. import time
  2. import threading
  3. import tkinter as tk
  4. from tkinter.messagebox import *
  5. from tkinter.ttk import Combobox, Progressbar
  6. import pygame


  7. def Top1():
  8.     global top1, entry1, entry2, combobox1
  9.     try:
  10.         top1.destroy()
  11.     except:
  12.         pass

  13.     top1 = tk.Toplevel(win)
  14.     top1.geometry("250x250")
  15.     top1.title("闹钟")

  16.     label1_1 = tk.Label(top1, text="当前时间:", font=("黑体", 15), height=2)
  17.     label1_1.grid(row=0, column=0)
  18.     label1_2 = tk.Label(top1, textvariable=varLabel1, font=("黑体", 15), height=2)
  19.     label1_2.grid(row=0, column=1)

  20.     label1_3 = tk.Label(top1, text="闹钟时间:", font=("黑体", 15), height=2)
  21.     label1_3.grid(row=1, column=0)
  22.     # 设定 justify 属性,其定义是输入控件中文本的对齐方式。
  23.     # 可以设置的数值为:LEFT,CENTER, RIGHT。默认数值是LEFT。
  24.     entry1 = tk.Entry(top1, font=("黑体", 15), width=13, bg="#FFEBCD", justify='center')
  25.     entry1.insert(0, now)
  26.     entry1.grid(row=1, column=1)

  27.     label4 = tk.Label(top1, text="提示词:", font=("黑体", 15), height=2)
  28.     label4.grid(row=2, column=0)
  29.     entry2 = tk.Entry(top1, textvariable=varLabel2,font=("微软雅黑 Light", 15),
  30.                    width=13, bg="#FFEBCD")
  31.     entry2.grid(row=2, column=1)

  32.     label5 = tk.Label(top1, text="闹钟音乐:", font=("黑体", 15), height=2)
  33.     label5.grid(row=3, column=0)
  34.     combobox1 = Combobox(top1,values=music, width=12, font=("楷体", 13))
  35.     combobox1.current(0)  # current(index):设置默认选中索引为index的选项
  36.     combobox1.grid(row=3, column=1)

  37.     ok_button_1 = tk.Button(top1, text="确定",font=25, width=10, bg="#00f5ff", command=ok_time)
  38.     ok_button_1.grid(row=4, column=0, columnspan=2, pady=30)


  39. def Top2():
  40.     global top2, last_h, last_m, last_s, last_ms, clock_is_running, start_or_pause_button
  41.     try:
  42.         top2.destroy()
  43.     except:
  44.         pass

  45.     top2 = tk.Toplevel(win)
  46.     top2.geometry("335x425")
  47.     top2.title("秒表")
  48.     last_h = last_m = last_s = last_ms = 0
  49.     last_time_label = tk.Label(top2, textvariable=last_time, font=("微软雅黑 Light", 40))
  50.     last_time_label.grid(row=0, column=0, columnspan=3)

  51.     start_or_pause_button = tk.Button(top2, text="开始", font=("黑体", 15), command=clock_start_or_pause)
  52.     start_or_pause_button.grid(row=1, column=0)
  53.     remember_button = tk.Button(top2, text="计次", font=("黑体", 15), command=remember_last_time)
  54.     remember_button.grid(row=1, column=1)
  55.     reset_button = tk.Button(top2, text="重置", font=("黑体", 15), command=reset_clock)
  56.     reset_button.grid(row=1, column=2)


  57. def Top3():
  58.     global top3, count_pro
  59.     try:
  60.         top3.destroy()
  61.     except:
  62.         pass

  63.     top3 = tk.Toplevel(win)
  64.     top3.geometry("400x400")

  65.     label3_1 = tk.Label(top3, text="定时器时长(时:分:秒)", font=("黑体", 15), height=2)
  66.     label3_1.grid(row=0, column=0, columnspan=3)

  67.     sc1 = tk.Scale(top3, length=200, label=":", from_=0, to=23, tickinterval=2)
  68.     sc1.grid(row=1, column=0)

  69.     sc2 = tk.Scale(top3, length=200, label=":", from_=0, to=59, tickinterval=10)
  70.     sc2.grid(row=1, column=1)

  71.     sc3 = tk.Scale(top3, length=200, from_=0, to=59, tickinterval=10)
  72.     sc3.grid(row=1, column=2)

  73.     count_pro = Progressbar(top3, value=0, length=200, maximum=1000)
  74.     count_pro.grid(row=2, column=0, columnspan=3, pady=15)

  75.     ok_button_3 = tk.Button(top3, text="确定", font=25, width=10, bg="#00f5ff",
  76.                          command=lambda: count(sc1.get(), sc2.get(), sc3.get()))
  77.     ok_button_3.grid(row=3, column=0, columnspan=3, pady=30)


  78. def clock_start_or_pause():
  79.     global clock_is_running, start_or_pause_button, times_box
  80.     clock_is_running = not clock_is_running
  81.     text_list = ["继续", "暂停"]
  82.     start_or_pause_button.config(text=text_list[clock_is_running])
  83.     times_box = tk.Listbox(top2, font=("微软雅黑", 18), fg="blue", width=20, height=10)
  84.     times_box.grid(row=3, column=0, columnspan=3)
  85.     clock_run()


  86. def clock_run():
  87.    
  88.     def job2():
  89.         global clock_is_running, last_h, last_m, last_s, last_ms
  90.         while clock_is_running:
  91.             set_h = str(last_h).zfill(2)
  92.             set_m = str(last_m).zfill(2)
  93.             set_s = str(last_s).zfill(2)
  94.             set_ms = str(last_ms).zfill(3)
  95.             last_time.set(set_h + ":" + set_m + ":" + set_s + "." + set_ms)

  96.             time.sleep(0.001)
  97.             last_ms += 1
  98.             if last_ms == 1000:
  99.                 last_ms = 0
  100.                 last_s += 1
  101.                 if last_s == 60:
  102.                     last_s = 0
  103.                     last_m += 1
  104.                     if last_m == 1000:
  105.                         last_m = 0
  106.                         last_h += 1
  107.                         if last_h == 99:
  108.                             break
  109.         last_time.set("00:00:00.000")
  110.     clock_job = threading.Thread(target=job2)
  111.     clock_job.start()


  112. def remember_last_time():
  113.     global times
  114.     times += 1
  115.     times_box.insert(times, "计次" + str(times) + "-" + last_time.get())


  116. def reset_clock():
  117.     global clock_is_running, last_h, last_m, last_s, last_ms, times, last_time
  118.     times_box.destroy()
  119.     clock_is_running = False
  120.     last_h, last_m, last_s, last_ms, times = 0, 0, 0, 0, 0
  121.     start_or_pause_button.config(text="开始")


  122. def count(h, m, s):
  123.     total_seconds = h * 3600 + m * 60 + s

  124.     def job():
  125.         count_pro.start(total_seconds)  # total_seconds*1000/1000
  126.         time.sleep(total_seconds)

  127.         count_pro.stop()
  128.         showinfo("嘿", "倒计时结束了")
  129.         top3.destroy()

  130.     count_job = threading.Thread(target=job)
  131.     count_job.start()


  132. def now_time():
  133.     global now
  134.     now = time.strftime('%H:%M:%S')
  135.     varLabel1.set(now)
  136.     win.after(1000, now_time)


  137. def ok_time():
  138.     if entry1.get() == "":
  139.         showinfo("", "输入为空")
  140.     else:
  141.         top1.iconify()
  142.         if now == entry1.get():
  143.             pygame.mixer.music.load(combobox1.get())
  144.             pygame.mixer.music.play(-1)
  145.             showinfo("It's time to...", "到点了~%s\n%s" %(now,entry2.get()))
  146.             pygame.mixer.music.stop()
  147.             top1.destroy()

  148.         top1.after(1000, ok_time)


  149. music = ["清脆.mp3", "大爆炸.wav"]

  150. pygame.init()

  151. pygame.mixer.music.set_volume(0.2)


  152. win = tk.Tk()
  153. varLabel1 = tk.StringVar()
  154. varLabel2 = tk.StringVar()
  155. varLabel2.set("闹钟")
  156. clock_is_running = False
  157. last_time = tk.StringVar()
  158. last_time.set("00:00:00.000")
  159. times = 0

  160. win.geometry("700x350")
  161. nowday = time.strftime('%Y-%m-%d %a.')
  162. now = time.strftime('%H:%M:%S')

  163. day_label = tk.Label(win, text=nowday, font=("微软雅黑", 30))
  164. day_label.grid(row=0, column=0, columnspan=3)
  165. time_label = tk.Label(win, textvariable=varLabel1, font=("微软雅黑", 60), fg="red")
  166. time_label.grid(row=1, column=0, columnspan=3)
  167. button1 = tk.Button(win, text="闹钟", width=20, height=3, font=20, command=Top1)
  168. button1.grid(row=2, column=0, padx=30, pady=30)
  169. button2 = tk.Button(win, text="秒表", width=20, height=3, font=20, command=Top2)
  170. button2.grid(row=2, column=1, padx=30, pady=30)
  171. button3 = tk.Button(win, text="倒计时", width=20, height=3, font=20, command=Top3)
  172. button3.grid(row=2, column=2, padx=30, pady=30)

  173. now_time()

  174. win.mainloop()
复制代码

欢迎各位大佬评分

评分

参与人数 2荣誉 +8 鱼币 +8 贡献 +6 收起 理由
python爱好者. + 5 + 5 + 3 鱼C有你更精彩 ^_^
~风介~ + 3 + 3 + 3 鱼C有你更精彩^_^

查看全部评分

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-5-15 12:29:29 | 显示全部楼层
很棒的代码!你成功地制作了一个电子钟的程序。

不过我有几点建议:
1. 在导入模块时,最好将`import time`与其他的`import`语句分开,可读性会更好。
2. 在函数`Top1()`中,可以使用`top1.lift()`代替`top1.iconify()`,这样就不会最小化窗口,而是将其置前。
3. 在`ok_time()`函数中,应该考虑一种特殊情况,即当用户设置的闹钟时间已经过去了。目前的代码只判断了两个时间是否相等,而没有判断是否大于当前时间。可以使用`time.strptime()`函数将字符串转换为时间对象,然后比较两个时间对象的大小。
4. 最后,你可以添加注释来解释代码的功能和细节,这样别人阅读你的代码时会更容易理解。

总体来说,这是一个优秀的项目!继续加油!如果还有其他问题,欢迎随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-5-15 18:52:39 | 显示全部楼层
第二版就要来了没人看吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-5-15 18:52:44 | 显示全部楼层
某一个“天” 发表于 2024-05-15 18:52
第二版就要来了没人看吗


抱歉,可能之前回答的有点晚了。没问题!我很乐意帮助你看第二版的代码,请继续发给我。我会尽快为你提供反馈和建议!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-5-15 18:54:36 | 显示全部楼层

回帖奖励 +2 鱼币

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2024-5-15 19:20:53 | 显示全部楼层
tkinter电子钟,第二版--修了bug 加上了花里胡哨的颜色
https://fishc.com.cn/thread-242901-1-1.html
(出处: 鱼C论坛)
第二版已经出啦
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-5-15 19:27:25 | 显示全部楼层
谢谢大家支持~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-5-16 17:02:08 | 显示全部楼层
这段代码实现了一个功能相对完整的桌面应用程序,集成了闹钟、秒表和倒计时三大功能,使用了Tkinter库进行图形界面设计,并结合了Pygame库来播放音乐。下面是对现有代码的一些建议和改进方向,旨在提高代码的可维护性、用户体验及稳定性。

### 1. 优化全局变量管理

- 尽量减少全局变量的使用,可以考虑将相关变量封装进类中,比如创建一个`ClockApp`类来管理所有组件和状态,这样可以提高代码的模块化和可读性。

### 2. 异常处理

- 在调用`try-except`块时,最好明确捕获预期的异常类型,而不是捕获所有异常。例如,在尝试销毁窗口时,可以捕获`TclError`来处理窗口未创建的情况。

### 3. 增强用户体验

- 在闹钟功能中,当设定的时间到达时,除了弹窗提醒,还可以考虑使用更温和的通知方式,比如托盘通知,避免打扰用户正在进行的工作。
- 秒表和倒计时功能结束后,可以加入简单的声音提示,利用Pygame库播放一个短促的声音。

### 4. 资源管理

- 确保音频文件正确关闭,尤其是在多次播放音乐的场景下。虽然Pygame的音乐模块在曲目结束时通常会自动停止,但在程序设计中显式管理资源的释放是个好习惯。

### 5. 线程安全

- 多线程环境下访问GUI组件(如修改标签的文本)需要小心处理,Tkinter并非线程安全。可以使用`queue`模块来传递信息给主线程更新GUI,避免直接在子线程中操作GUI组件。

### 6. 代码复用和模块化

- 抽取重复的代码片段到函数或方法中,比如时间格式化、窗口布局的通用部分等。
- 对于音乐播放逻辑,可以封装成一个独立的类或方法,方便管理和复用。

### 7. 文档和注释

- 添加必要的注释,尤其是对于复杂的逻辑处理和非直观的代码段,帮助理解代码意图。
- 在每个功能模块的开始处添加简短的描述性注释,概述该部分的主要功能和结构。

### 示例改进思路

```python
class ClockApp(tk.Tk):
    def __init__(self):
        super().__init__()
        self.initUI()
        self.now_time()

    def initUI(self):
        # 初始化UI代码...
        pass

    def now_time(self):
        # 更新时间的逻辑...
        pass

    # 类似地,为Top1, Top2, Top3等功能创建方法
    def show_top1(self):
        # 闹钟界面逻辑...
        pass

    # 其他功能方法...

if __name__ == "__main__":
    app = ClockApp()
    app.mainloop()
```

通过以上改进思路,可以使代码更加整洁、易于维护,并提升用户体验。希望这些建议对你有所帮助!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-5-16 17:04:28 | 显示全部楼层

回帖奖励 +2 鱼币

不错
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

头像被屏蔽
发表于 2024-6-5 22:58:47 | 显示全部楼层

回帖奖励 +2 鱼币

提示: 作者被禁止或删除 内容自动屏蔽
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-6-11 15:21:32 | 显示全部楼层

回帖奖励 +2 鱼币


学习
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-8-17 17:04:31 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-8-17 17:05:10 | 显示全部楼层

回帖奖励 +2 鱼币

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2024-8-19 15:52:28 | 显示全部楼层
第三版精华啦
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-11 19:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表