如何使用Python调用ChatGPT来开发基于Tkinter的桌面时钟?

小磊同学_4758

小磊同学_4758

2023-05-08

1557人浏览

转载

描述

给chatgpt的描述内容:

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

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

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

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

Python健壮编程
Python健壮编程

编写可靠Python代码,避免可变默认值、导入陷阱与运行时意外

下载

窗口中的内容居中显示。

代码

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

import tkinter as tk
import datetime
import math
import locale
 
# Set the locale to use UTF-8 encoding
locale.setlocale(locale.LC_ALL, 'en_US.utf8')
 
 
class TransparentWindow(tk.Tk):
    def __init__(self, text_file=None):
        super().__init__()
        self.attributes('-alpha', 1) # 设置窗口透明度
        # self.attributes('-topmost', True) # 窗口置顶
        # self.attributes('-transparentcolor', '#000000')
        self.overrideredirect(True) # 去掉窗口边框
        self.locked = False # 初始化锁定状态
        self.mouse_x = 0
        self.mouse_y = 0
        self.config(bg='#000000', highlightthickness=0, bd=0)
        
        
        # 获取屏幕尺寸和窗口尺寸,使窗口居中
        screen_width = self.winfo_screenwidth()
        screen_height = self.winfo_screenheight()
        window_width = 400
        window_height = 100
        x = (screen_width - window_width) // 2
        y = (screen_height - window_height) // 2
        self.geometry('{}x{}+{}+{}'.format(window_width, window_height, x, y))
 
        # 添加日期时间标签
        self.datetime_label = tk.Label(self, text='', font=('Arial', 20), fg='#FFFFFF', bg='#000000')
        self.datetime_label.place(relx=0.5, y=20, anchor='center')
 
        # 提示标签
        self.note_label = tk.Label(self, text='123', font=('Arial', 14), fg='#FFFFFF', bg='#000000')
        self.note_label.place(relx=0.5, y=50, anchor='center')
 
        # 文本标签
        self.text_label = tk.Label(self, text='', font=('Arial', 14), fg='#FFFFFF', bg='#000000')
        self.text_label.place(relx=0.5, y=80, anchor='center')
 
        # 添加锁定按钮
        self.lock_button = tk.Button(self, text='锁定', font=('Arial', 10), command=self.toggle_lock)
        self.toggle_lock_button(True)
        self.toggle_lock_button(False)
 
        # 添加解锁按钮
        self.unlock_button = tk.Button(self, text='解除锁定', font=('Arial', 10), command=self.toggle_lock)
        self.toggle_unlock_button(True)
        self.toggle_unlock_button(False)
 
        # 定时更新日期时间标签
        self.update_datetime()
        # 定时更新text标签
        self.update_text_label()
        # 定时更新note标签
        self.update_note_label()
 
        # 绑定鼠标事件
        self.bind('<button-1>', self.on_left_button_down)
        self.bind('<buttonrelease-1>', self.on_left_button_up)
        self.bind('<b1-motion>', self.on_mouse_drag)
        self.bind('<enter>', self.on_mouse_enter)
        self.bind('<leave>', self.on_mouse_leave)
 
        # 创建右键菜单
        self.menu = tk.Menu(self, tearoff=0)
        self.menu.add_command(label="退出", command=self.quit)
        self.bind("<button-3>", self.show_menu)
 
 
    def toggle_lock_button(self, show=True):
        if show:
            self.lock_button.place(relx=1, rely=0.85, anchor='e')
        else:
            self.lock_button.place_forget()
    
    def toggle_unlock_button(self, show=True):
        if show:
            self.unlock_button.place(relx=1, rely=0.85, anchor='e')
        else:
            self.unlock_button.place_forget()
 
    def show_menu(self, event):
        self.menu.post(event.x_root, event.y_root)
 
    def update_datetime(self):
        now = datetime.datetime.now().strftime('%Y-%m-%d     \u270d     %H:%M:%S.%f')[:-4]
        msg = f'{now}'
        self.datetime_label.configure(text=msg)
        self.after(10, self.update_datetime)
 
    def update_text_label(self):
        now = '小锋学长生活大爆炸'
        self.text_label.configure(text=now)
        self.after(1000, self.update_text_label)
 
    def update_note_label(self):
        # 指定日期,格式为 年-月-日
        specified_start_date = datetime.date(2023, 2, 20)
        specified_end_date = datetime.date(2023, 7, 9)
        today = datetime.date.today()
        # 计算距离指定日期过了多少周
        start_delta = today - specified_start_date
        num_of_weeks = math.ceil(start_delta.days / 7)
        # 计算距离指定日期剩余多少周
        end_delta = specified_end_date - today
        remain_weeks = math.ceil(end_delta.days / 7)
 
        msg = f'当前第{num_of_weeks}周, 剩余{remain_weeks}周({end_delta.days}天)'
        self.note_label.configure(text=msg)
        self.after(1000*60, self.update_note_label)
 
 
    def toggle_lock(self):
        if self.locked:
            self.locked = False
            self.toggle_lock_button(True)
            self.toggle_unlock_button(False)
        else:
            self.locked = True
            self.toggle_lock_button(False)
            self.toggle_unlock_button(True)
 
    def on_left_button_down(self, event):
        self.mouse_x = event.x
        self.mouse_y = event.y
 
    def on_left_button_up(self, event):
        self.mouse_x = 0
        self.mouse_y = 0
 
    def on_mouse_drag(self, event):
        if not self.locked:
            x = self.winfo_x() + event.x - self.mouse_x
            y = self.winfo_y() + event.y - self.mouse_y
            self.geometry('+{}+{}'.format(x, y))
 
    def on_mouse_leave(self, event):
        self.lock_button.place_forget()
        self.unlock_button.place_forget()
 
    def on_mouse_enter(self, event):
        if not self.locked:
            self.toggle_lock_button(True)
            self.toggle_unlock_button(False)
        else:
            self.toggle_lock_button(False)
            self.toggle_unlock_button(True)
 
 
if __name__ == '__main__':
    app = TransparentWindow(text_file='text.txt')
    app.mainloop()</button-3></leave></enter></b1-motion></buttonrelease-1></button-1>

Python免费学习笔记(深入):立即使用
在学习笔记中,你将探索 Python 的核心概念和高级技巧!

相关文章

python速学教程(入门到精通)
python速学教程(入门到精通)

python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

chatgpt python 桌面时钟

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
class在c语言中的意思
class在c语言中的意思

在C语言中,"class" 是一个关键字,用于定义一个类。想了解更多class的相关内容,可以阅读本专题下面的文章。

2024.01.03

4756

7

python中class的含义
python中class的含义

本专题整合了python中class的相关内容,阅读专题下面的文章了解更多详细内容。

2025.12.06

326

9

ChatGPT注册
ChatGPT注册

ChatGPT注册方法:1、访问OpenAI的官方网站,进入注册页面;2、完成注册后收到一份邮件,打开后点击验证账号;3、选择一个适合您需求的订阅计划;4、获得访问ChatGPT的权限即可。

2023.09.12

2813

9

国内免费ChatGPT大全
国内免费ChatGPT大全

ChatGPT是一种基于深度学习技术的自然语言处理模型,由OpenAI开发。它是GPT的一个变体,专门设计用于生成上下文相关的文本回复。ChatGPT被训练成一个聊天机器人,可以与用户进行对话交互。更多关于ChatGPT的文章详情请查看本专题,希望对大家能有所帮助。

2023.10.25

7085

20

手机安装chatgpt的方法
手机安装chatgpt的方法

手机安装chatgpt的方法:1、在ChatGTP官网或手机商店上下载ChatGTP软件;2、打开后在设置界面中,选择语言为中文;3、在对局界面中,选择人机对局并设置中文相谱;4、开始后在聊天窗口中输入指令,即可与软件进行交互。想了解更多chatgpt的相关内容,可以阅读本专题下面的文章。

2024.03.05

10561

9

chatgpt国内可不可以使用
chatgpt国内可不可以使用

chatgpt在国内可以使用,但不能注册,港澳也不行,用户想要注册的话,可以使用国外的手机号进行注册,注意注册过程中要将网络环境切换成国外ip。想了解更多chatgpt的相关内容,可以阅读本专题下面的文章。

2024.03.05

4176

10

chatgpt官网入口地址合集
chatgpt官网入口地址合集

本专题整合了chatgpt官网入口地址、使用教程等内容,阅读专题下面的文章了解更多详细内容。

2026.03.16

1958

24

chatgpt使用指南
chatgpt使用指南

本专题整合了chatgpt使用教程、新手使用说明等等相关内容,阅读专题下面的文章了解更多详细内容。

2026.03.16

677

20

ChatGPT编程开发实战教程
ChatGPT编程开发实战教程

本专题全面讲解ChatGPT在编程开发中的应用,包括代码生成、接口调试、Bug修复、前端页面开发、Python脚本编写及数据库优化等,帮助开发者提升效率。

2026.05.29

320

14

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
ChatGPT入门手册
ChatGPT入门手册

共0课时 | 0人学习

ChatGPT使用教学
ChatGPT使用教学

共2课时 | 254人学习