Python怎么调用ChatGPT制作基于Tkinter的桌面时钟

其他教程   发布日期:2025年02月07日   浏览次数:234

本文小编为大家详细介绍“Python怎么调用ChatGPT制作基于Tkinter的桌面时钟”,内容详细,步骤清晰,细节处理妥当,希望这篇“Python怎么调用ChatGPT制作基于Tkinter的桌面时钟”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

描述

给ChatGPT的描述内容:

python在桌面上显示动态的文字,不要显示窗口边框。窗口背景和标签背景都是透明的,但标签内的文字是有颜色。使用tkinter库实现,并以class的形式书写,方便用户对内容进行扩展开发。

窗口默认出现在屏幕的中间位置。窗口中的标签需要包含两项内容。其中一项用于实时显示当前的日期和时间,精确到毫秒。另一项从txt文件中读取显示,若没有txt文件则显示“None”。

在未锁定状态下,鼠标可以拖动窗口。在锁定状态下,窗口无法通过鼠标的拖动而移动。在窗口中添加一个“锁定”按钮,当鼠标移动到窗口上方时,显示“锁定”按钮,鼠标移走后,隐藏“锁定”按钮。通过“锁定”按钮,窗口进入锁定状态。在锁定状态下,当鼠标移动到窗口上方时,显示一个“解除锁定”的按钮,鼠标移走后,隐藏该“解除锁定”按钮。通过点击“解除锁定”按钮,进入未锁定状态。锁定和未锁定状态是互相切换的。

给窗口添加一个鼠标右键的功能,在右键菜单中,可以点击“退出”,从而退出应用。

窗口中的内容居中显示。

代码

给出的代码,并经过微调:

  1. import tkinter as tk
  2. import datetime
  3. import math
  4. import locale
  5. # Set the locale to use UTF-8 encoding
  6. locale.setlocale(locale.LC_ALL, 'en_US.utf8')
  7. class TransparentWindow(tk.Tk):
  8. def __init__(self, text_file=None):
  9. super().__init__()
  10. self.attributes('-alpha', 1) # 设置窗口透明度
  11. # self.attributes('-topmost', True) # 窗口置顶
  12. # self.attributes('-transparentcolor', '#000000')
  13. self.overrideredirect(True) # 去掉窗口边框
  14. self.locked = False # 初始化锁定状态
  15. self.mouse_x = 0
  16. self.mouse_y = 0
  17. self.config(bg='#000000', highlightthickness=0, bd=0)
  18. # 获取屏幕尺寸和窗口尺寸,使窗口居中
  19. screen_width = self.winfo_screenwidth()
  20. screen_height = self.winfo_screenheight()
  21. window_width = 400
  22. window_height = 100
  23. x = (screen_width - window_width) // 2
  24. y = (screen_height - window_height) // 2
  25. self.geometry('{}x{}+{}+{}'.format(window_width, window_height, x, y))
  26. # 添加日期时间标签
  27. self.datetime_label = tk.Label(self, text='', font=('Arial', 20), fg='#FFFFFF', bg='#000000')
  28. self.datetime_label.place(relx=0.5, y=20, anchor='center')
  29. # 提示标签
  30. self.note_label = tk.Label(self, text='123', font=('Arial', 14), fg='#FFFFFF', bg='#000000')
  31. self.note_label.place(relx=0.5, y=50, anchor='center')
  32. # 文本标签
  33. self.text_label = tk.Label(self, text='', font=('Arial', 14), fg='#FFFFFF', bg='#000000')
  34. self.text_label.place(relx=0.5, y=80, anchor='center')
  35. # 添加锁定按钮
  36. self.lock_button = tk.Button(self, text='锁定', font=('Arial', 10), command=self.toggle_lock)
  37. self.toggle_lock_button(True)
  38. self.toggle_lock_button(False)
  39. # 添加解锁按钮
  40. self.unlock_button = tk.Button(self, text='解除锁定', font=('Arial', 10), command=self.toggle_lock)
  41. self.toggle_unlock_button(True)
  42. self.toggle_unlock_button(False)
  43. # 定时更新日期时间标签
  44. self.update_datetime()
  45. # 定时更新text标签
  46. self.update_text_label()
  47. # 定时更新note标签
  48. self.update_note_label()
  49. # 绑定鼠标事件
  50. self.bind('<Button-1>', self.on_left_button_down)
  51. self.bind('<ButtonRelease-1>', self.on_left_button_up)
  52. self.bind('<B1-Motion>', self.on_mouse_drag)
  53. self.bind('<Enter>', self.on_mouse_enter)
  54. self.bind('<Leave>', self.on_mouse_leave)
  55. # 创建右键菜单
  56. self.menu = tk.Menu(self, tearoff=0)
  57. self.menu.add_command(label="退出", command=self.quit)
  58. self.bind("<Button-3>", self.show_menu)
  59. def toggle_lock_button(self, show=True):
  60. if show:
  61. self.lock_button.place(relx=1, rely=0.85, anchor='e')
  62. else:
  63. self.lock_button.place_forget()
  64. def toggle_unlock_button(self, show=True):
  65. if show:
  66. self.unlock_button.place(relx=1, rely=0.85, anchor='e')
  67. else:
  68. self.unlock_button.place_forget()
  69. def show_menu(self, event):
  70. self.menu.post(event.x_root, event.y_root)
  71. def update_datetime(self):
  72. now = datetime.datetime.now().strftime('%Y-%m-%d u270d %H:%M:%S.%f')[:-4]
  73. msg = f'{now}'
  74. self.datetime_label.configure(text=msg)
  75. self.after(10, self.update_datetime)
  76. def update_text_label(self):
  77. now = '小锋学长生活大爆炸'
  78. self.text_label.configure(text=now)
  79. self.after(1000, self.update_text_label)
  80. def update_note_label(self):
  81. # 指定日期,格式为 年-月-日
  82. specified_start_date = datetime.date(2023, 2, 20)
  83. specified_end_date = datetime.date(2023, 7, 9)
  84. today = datetime.date.today()
  85. # 计算距离指定日期过了多少周
  86. start_delta = today - specified_start_date
  87. num_of_weeks = math.ceil(start_delta.days / 7)
  88. # 计算距离指定日期剩余多少周
  89. end_delta = specified_end_date - today
  90. remain_weeks = math.ceil(end_delta.days / 7)
  91. msg = f'当前第{num_of_weeks}周, 剩余{remain_weeks}周({end_delta.days}天)'
  92. self.note_label.configure(text=msg)
  93. self.after(1000*60, self.update_note_label)
  94. def toggle_lock(self):
  95. if self.locked:
  96. self.locked = False
  97. self.toggle_lock_button(True)
  98. self.toggle_unlock_button(False)
  99. else:
  100. self.locked = True
  101. self.toggle_lock_button(False)
  102. self.toggle_unlock_button(True)
  103. def on_left_button_down(self, event):
  104. self.mouse_x = event.x
  105. self.mouse_y = event.y
  106. def on_left_button_up(self, event):
  107. self.mouse_x = 0
  108. self.mouse_y = 0
  109. def on_mouse_drag(self, event):
  110. if not self.locked:
  111. x = self.winfo_x() + event.x - self.mouse_x
  112. y = self.winfo_y() + event.y - self.mouse_y
  113. self.geometry('+{}+{}'.format(x, y))
  114. def on_mouse_leave(self, event):
  115. self.lock_button.place_forget()
  116. self.unlock_button.place_forget()
  117. def on_mouse_enter(self, event):
  118. if not self.locked:
  119. self.toggle_lock_button(True)
  120. self.toggle_unlock_button(False)
  121. else:
  122. self.toggle_lock_button(False)
  123. self.toggle_unlock_button(True)
  124. if __name__ == '__main__':
  125. app = TransparentWindow(text_file='text.txt')
  126. app.mainloop()

以上就是Python怎么调用ChatGPT制作基于Tkinter的桌面时钟的详细内容,更多关于Python怎么调用ChatGPT制作基于Tkinter的桌面时钟的资料请关注九品源码其它相关文章!