鱼C论坛

 找回密码
 立即注册
查看: 6513|回复: 66

[技术交流] 第三版电子钟震撼来袭!炫酷变色效果,多线程灵动使用

[复制链接]
发表于 2024-7-31 18:22:22 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 某一个“天” 于 2024-8-16 09:50 编辑

设计思路:
刚开始只是写着玩玩,后来发现这时钟挺有意思,没多想就扩展了许多功能。顺便学了一下多线程,
其实只要打好基础,这个真的很简单~(甲鱼哥的教程是真的用心,节奏快,干货多
未来前景:
加上时钟表盘,优化多线程,完善计时器
程序解释:
首先介绍一下新加的渐变色效果:
  1. def make_it_more_beautiful():
  2.     # 矩形属性
  3.     rect = canvas.create_rectangle(10, 10, 580, 350, outline='red', width=40)
  4.     # RGB颜色循环
  5.     r, g, b = 255, 0, 0  # 初始颜色为红色

  6.     while True:
  7.         # 每0.1秒更新一次颜色
  8.         time.sleep(0.1)
  9.         # RGB颜色渐变
  10.         r = (r - 1) % 256
  11.         g = (g + 2) % 256
  12.         b = (b + 1) % 256

  13.         # 更新矩形颜色
  14.         canvas.itemconfig(rect, outline=f'#{r:02x}{g:02x}{b:02x}')
复制代码

就是画一个初始边缘颜色为红的矩形,每 0.1 秒变一次色,但并不是总丝滑


运行结束会有 RuntimeError,不用理?
因为tkinter其实不太支持多线程,而after有时出错!

最后是完整代码,想试一下的记得准备音乐
  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 make_it_more_beautiful():
  8.     # 矩形属性
  9.     rect = canvas.create_rectangle(10, 10, 580, 350, outline='red', width=40)
  10.     # RGB颜色循环
  11.     r, g, b = 255, 0, 0  # 初始颜色为红色

  12.     while True:
  13.         # 每0.1秒更新一次颜色
  14.         time.sleep(0.1)
  15.         # RGB颜色渐变
  16.         r = (r - 1) % 256
  17.         g = (g + 2) % 256
  18.         b = (b + 1) % 256

  19.         # 更新矩形颜色
  20.         canvas.itemconfig(rect, outline=f'#{r:02x}{g:02x}{b:02x}')


  21. def clock_start_or_pause():
  22.     global clock_is_running, start_or_pause_button, reset
  23.     clock_is_running = not clock_is_running
  24.     reset = False
  25.     text_list = ["继续", "暂停"]
  26.     start_or_pause_button.config(text=text_list[clock_is_running])

  27.     clock_run()


  28. def clock_run():
  29.     def job2():
  30.         global clock_is_running, last_h, last_m, last_s, last_ms
  31.         while clock_is_running:
  32.             set_h = str(last_h).zfill(2)
  33.             set_m = str(last_m).zfill(2)
  34.             set_s = str(last_s).zfill(2)
  35.             set_ms = str(last_ms).zfill(3)
  36.             var_last_time.set(set_h + ":" + set_m + ":" + set_s + "." + set_ms)

  37.             time.sleep(0.001)
  38.             last_ms += 1
  39.             if last_ms == 1000:
  40.                 last_ms = 0
  41.                 last_s += 1
  42.                 if last_s == 60:
  43.                     last_s = 0
  44.                     last_m += 1
  45.                     if last_m == 1000:
  46.                         last_m = 0
  47.                         last_h += 1
  48.         if reset:
  49.             var_last_time.set("00:00:00.000")

  50.     clock_job = threading.Thread(target=job2)
  51.     clock_job.start()


  52. def remember_last_time():
  53.     global times
  54.     times += 1
  55.     times_box.insert(times, "计次" + str(times) + "-" + var_last_time.get())


  56. def reset_clock():
  57.     global clock_is_running, last_h, last_m, last_s, last_ms, times, var_last_time, times_box, reset
  58.     times_box.delete(0, tk.END)
  59.     reset = True
  60.     clock_is_running = False
  61.     last_h, last_m, last_s, last_ms, times = 0, 0, 0, 0, 0
  62.     start_or_pause_button.config(text="开始")
  63.     var_last_time.set("00:00:00.000")


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

  66.     def job():
  67.         count_pro.start(total_seconds)  # total_seconds*1000/1000
  68.         time.sleep(total_seconds)

  69.         count_pro.stop()
  70.         showinfo("嘿", "倒计时结束了")
  71.         top3.destroy()

  72.     count_job = threading.Thread(target=job)
  73.     count_job.start()


  74. def now_time():
  75.     global var_now, var_nowday
  76.     var_now.set(time.strftime('%H:%M:%S'))
  77.     # 考虑到程序持续到第二天
  78.     if var_now == "00:00:00":
  79.         var_nowday.set(time.strftime("%Y-%m-%d %a."))
  80.     win.after(1000, now_time)


  81. def ok_time():
  82.     if entry1.get() == "":
  83.         showinfo("", "输入为空")
  84.     else:
  85.         top1.iconify()
  86.         if var_now.get() == entry1.get():
  87.             pygame.mixer.music.load(combobox1.get())
  88.             pygame.mixer.music.play(-1)
  89.             showinfo(f"It's time to...", f"到点了~{var_now.get()}\n{entry2.get()}")
  90.             pygame.mixer.music.stop()
  91.             top1.destroy()

  92.         top1.after(1000, ok_time)


  93. def Top1():
  94.     global top1, entry1, entry2, combobox1
  95.     try:
  96.         top1.destroy()
  97.     except:
  98.         pass

  99.     top1 = tk.Toplevel(win)
  100.     top1.geometry("250x250")
  101.     top1.title("闹钟")

  102.     label1_1 = tk.Label(top1, text="当前时间:", font=("黑体", 15), height=2)
  103.     label1_1.grid(row=0, column=0)
  104.     label1_2 = tk.Label(top1, textvariable=var_now, font=("黑体", 15), height=2)
  105.     label1_2.grid(row=0, column=1)

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

  113.     label4 = tk.Label(top1, text="提示词:", font=("黑体", 15), height=2)
  114.     label4.grid(row=2, column=0)
  115.     entry2 = tk.Entry(top1, textvariable=var_tip_label, font=("微软雅黑 Light", 15),
  116.                       width=13, bg="#FFEBCD")
  117.     entry2.grid(row=2, column=1)

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

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


  125. def Top2():
  126.     global top2, clock_is_running, start_or_pause_button, times_box
  127.     try:
  128.         top2.destroy()
  129.     except:
  130.         pass

  131.     top2 = tk.Toplevel(win)
  132.     top2.geometry("335x425")
  133.     top2.title("秒表")

  134.     last_time_label = tk.Label(top2, textvariable=var_last_time, font=("微软雅黑 Light", 40))
  135.     last_time_label.grid(row=0, column=0, columnspan=3)

  136.     start_or_pause_button = tk.Button(top2, text="开始", font=("黑体", 15), bg="green", command=clock_start_or_pause)
  137.     start_or_pause_button.grid(row=1, column=0)
  138.     remember_button = tk.Button(top2, text="计次", font=("黑体", 15), fg="purple", command=remember_last_time)
  139.     remember_button.grid(row=1, column=1)
  140.     reset_button = tk.Button(top2, text="重置", font=("黑体", 15), bg="orange", command=reset_clock)
  141.     reset_button.grid(row=1, column=2)

  142.     times_box = tk.Listbox(top2, font=("微软雅黑", 18), fg="blue", width=20, height=10)
  143.     times_box.grid(row=3, column=0, columnspan=3)


  144. def Top3():
  145.     global top3, count_pro
  146.     try:
  147.         top3.destroy()
  148.     except:
  149.         pass

  150.     top3 = tk.Toplevel(win)
  151.     top3.geometry("260x380")

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

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

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

  158.     sc3 = tk.Scale(top3, length=200, from_=0, to=59, tickinterval=10, bg="orange")
  159.     sc3.grid(row=1, column=2)

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

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


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

  166. pygame.init()
  167. pygame.mixer.music.set_volume(0.5)

  168. win = tk.Tk()
  169. var_nowday = tk.StringVar()
  170. var_now = tk.StringVar()
  171. var_tip_label = tk.StringVar()
  172. var_tip_label.set("闹钟")
  173. clock_is_running = False
  174. reset = False
  175. last_h = last_m = last_s = last_ms = 0
  176. var_last_time = tk.StringVar()
  177. var_last_time.set("00:00:00.000")
  178. times = 0
  179. win.geometry("587x357")
  180. # 创建Canvas
  181. canvas = tk.Canvas(win, width=600, height=370, bg="white")
  182. canvas.pack()
  183. beautiful_job = threading.Thread(target=make_it_more_beautiful)
  184. beautiful_job.start()

  185. var_nowday.set(time.strftime("%Y-%m-%d %a."))
  186. var_now.set(time.strftime("%H:%M:%S"))

  187. day_label = tk.Label(canvas, textvariable=var_nowday, font=("微软雅黑", 30))
  188. day_label.grid(row=0, column=0, columnspan=3)
  189. time_label = tk.Label(canvas, textvariable=var_now, font=("微软雅黑", 60), fg="red")
  190. time_label.grid(row=1, column=0, columnspan=3)
  191. button1 = tk.Button(canvas, text="闹钟", font=("微软雅黑", 15), width=10, height=3, command=Top1)
  192. button1.grid(row=2, column=0, padx=33, pady=45)
  193. button2 = tk.Button(canvas, text="秒表", font=("微软雅黑", 15), width=10, height=3, command=Top2)
  194. button2.grid(row=2, column=1, padx=33, pady=45)
  195. button3 = tk.Button(canvas, text="倒计时", font=("微软雅黑", 15), width=10, height=3, command=Top3)
  196. button3.grid(row=2, column=2, padx=33, pady=45)

  197. now_time()

  198. win.mainloop()
复制代码
悄悄求个评分~
@不二如是 @sfqxx @zhangjinxuan @python爱好者. @KeyError @isdkz @小甲鱼的三师弟 @ba21 @Twilight6 @洛十三 @三体人的智子
捕获1.PNG
捕获2.PNG

评分

参与人数 13荣誉 +60 鱼币 +58 贡献 +43 收起 理由
~风介~ + 5 + 5 + 5 无条件支持楼主!
Lynn_oyl + 1 + 1 鱼C有你更精彩^_^
kounasx + 5 + 5 + 3 鱼C有你更精彩^_^
lzaiz24 + 5 + 5 + 3 鱼C有你更精彩^_^
寒时enjoy + 5 + 5 + 3 很有意思
liuhongrun2022 + 5 + 5 + 3
小甲鱼的二师兄 + 2 + 2 + 3 鱼C有你更精彩^_^
cjjJasonchen + 5 + 5 + 3
三体人的智子 + 5 + 3 + 3 鱼C有你更精彩^_^
zhangjinxuan + 5 鱼C有你更精彩^_^

查看全部评分

本帖被以下淘专辑推荐:

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

使用道具 举报

发表于 2024-7-31 18:39:25 | 显示全部楼层

回帖奖励 +3 鱼币

时钟大全
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-7-31 19:01:09 | 显示全部楼层

回帖奖励 +3 鱼币

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

使用道具 举报

发表于 2024-7-31 19:07:10 | 显示全部楼层

回帖奖励 +3 鱼币

期待有更好的版本。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-7-31 19:21:48 From FishC Mobile | 显示全部楼层

回帖奖励 +3 鱼币

才3鱼币,太小气

评分

参与人数 1鱼币 +3 收起 理由
某一个“天” + 3 再来3个?

查看全部评分

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

使用道具 举报

 楼主| 发表于 2024-7-31 19:56:06 | 显示全部楼层
ba21 发表于 2024-7-31 19:07
期待有更好的版本。

有什么完善建议吗

点评

我很赞同!: 5.0
我很赞同!: 5
或许可以做个时钟表盘,会动的那种?  发表于 2024-8-1 06:41
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-8-1 09:52:07 | 显示全部楼层

回帖奖励 +3 鱼币

win7让我回忆起了几年前
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-8-1 12:18:39 | 显示全部楼层

回帖奖励 +3 鱼币

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

使用道具 举报

 楼主| 发表于 2024-8-1 13:19:30 | 显示全部楼层
歌者文明清理员 发表于 2024-8-1 09:52
win7让我回忆起了几年前

嘿嘿,马上就用win11继续开发了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-8-1 17:31:51 | 显示全部楼层

回帖奖励 +3 鱼币

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

使用道具 举报

发表于 2024-8-2 08:57:58 | 显示全部楼层

回帖奖励 +3 鱼币

有点东西,来看看
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-8-4 16:57:17 | 显示全部楼层

回帖奖励 +3 鱼币

把字体改成Con就完美了!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-8-4 17:43:22 | 显示全部楼层
KeyError 发表于 2024-8-4 16:57
把字体改成Con就完美了!

什么是Con
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-8-4 20:13:00 | 显示全部楼层

回帖奖励 +3 鱼币


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

使用道具 举报

发表于 2024-8-9 10:06:18 | 显示全部楼层
有点迟到,哈哈

祝你早日精华帖!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-8-15 15:52:11 From FishC Mobile | 显示全部楼层
哎哟 不错哦 马上试试

评分

参与人数 1鱼币 +5 收起 理由
某一个“天” + 5 喜欢的话评个分呗

查看全部评分

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

使用道具 举报

发表于 2024-8-15 15:52:17 From FishC Mobile | 显示全部楼层
效果很棒,赶紧用起来
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-8-15 15:52:47 From FishC Mobile | 显示全部楼层
狂拽酷炫那个什么炸天
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-8-15 15:53:17 | 显示全部楼层
这个效果我喜欢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-8-15 15:53:48 | 显示全部楼层
加油
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-21 22:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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