鱼C论坛

 找回密码
 立即注册
查看: 810|回复: 10

[技术交流] tkinter电子钟,第二版--修了bug 加上了花里胡哨的颜色

[复制链接]
发表于 2024-5-15 19:19:03 | 显示全部楼层 |阅读模式

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

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

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, clock_is_running, start_or_pause_button, times_box
  41.     try:
  42.         top2.destroy()
  43.     except:
  44.         pass

  45.     top2 = tk.Toplevel(win)
  46.     top2.geometry("335x425")
  47.     top2.title("秒表")

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

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

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


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

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

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

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

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

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

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

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


  79. def clock_start_or_pause():
  80.     global clock_is_running, start_or_pause_button, reset
  81.     clock_is_running = not clock_is_running
  82.     reset = False
  83.     text_list = ["继续", "暂停"]
  84.     start_or_pause_button.config(text=text_list[clock_is_running])

  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 reset:
  108.             last_time.set("00:00:00.000")
  109.     clock_job = threading.Thread(target=job2)
  110.     clock_job.start()


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


  115. def reset_clock():
  116.     global clock_is_running, last_h, last_m, last_s, last_ms, times, last_time, times_box, reset
  117.     times_box.delete(0, tk.END)
  118.     reset = True
  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.     last_time.set("00:00:00.000")


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

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

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

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


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


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

  149.         top1.after(1000, ok_time)


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

  151. pygame.init()

  152. pygame.mixer.music.set_volume(0.5)


  153. win = tk.Tk()
  154. varLabel1 = tk.StringVar()
  155. varLabel2 = tk.StringVar()
  156. varLabel2.set("闹钟")
  157. clock_is_running = False
  158. reset = False
  159. last_h = last_m = last_s = last_ms = 0
  160. last_time = tk.StringVar()
  161. last_time.set("00:00:00.000")
  162. times = 0
  163. win.geometry("580x350")
  164. nowday = time.strftime('%Y-%m-%d %a.')
  165. now = time.strftime('%H:%M:%S')

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

  176. now_time()

  177. win.mainloop()
复制代码

@不二如是 @KeyError @python爱好者. @sfqxx @zhangjinxuan @stumblemodel @cjjJasonchen @学习编程中的Ben @一点沙 @zhangchenyvn
欢迎评论

评分

参与人数 5荣誉 +21 鱼币 +24 贡献 +16 收起 理由
小甲鱼的三师弟 + 2 + 2 + 2 鱼C有你更精彩^_^
python爱好者. + 5 + 5 + 3 鱼C有你更精彩^_^
不二如是 + 2 + 10 + 3 鱼C有你更精彩^_^
zhangjinxuan + 7 + 7 + 5 鱼C有你更精彩^_^
zhangchenyvn + 5 + 3 鱼C有你更精彩^_^

查看全部评分

本帖被以下淘专辑推荐:

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

使用道具 举报

发表于 2024-5-15 20:31:26 | 显示全部楼层
确实经典
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-5-15 19:19:09 | 显示全部楼层
代码看起来功能完备,也没有明显的错误。不过,在使用`pygame.mixer.music`播放音乐的时候,需要确保音乐文件的路径是正确的,可执行文件和音乐文件在同一目录下。另外,记得要导入所需的模块,如`from tkinter.ttk import Combobox, Progressbar`和`import pygame`。

祝你编程愉快!

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

使用道具 举报

发表于 2024-6-1 15:40:57 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2024-6-1 18:37:09 | 显示全部楼层

谢谢你对我作品的认可

点评

我很赞同!: 5.0
我很赞同!: 5
  发表于 2024-6-1 23:26
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-6-3 08:31:09 | 显示全部楼层
厉害
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-6-3 14:57:07 | 显示全部楼层
写的太棒了,我只能大喊一句牛逼!牛逼!牛逼!就很火的那个主持人老外讲牛逼这个段子的发音,你懂的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-6-3 16:11:52 | 显示全部楼层
小肥狼haoran 发表于 2024-6-3 14:57
写的太棒了,我只能大喊一句牛逼!牛逼!牛逼!就很火的那个主持人老外讲牛逼这个段子的发音,你懂的{:10_2 ...

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

使用道具 举报

发表于 2024-6-5 21:37:57 | 显示全部楼层
学习了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-6-14 07:58:17 | 显示全部楼层
很不错的!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-21 15:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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