Python調(diào)用ChatGPT制作基于Tkinter的桌面時(shí)鐘
描述
給ChatGPT的描述內(nèi)容:
python在桌面上顯示動態(tài)的文字,不要顯示窗口邊框。窗口背景和標(biāo)簽背景都是透明的,但標(biāo)簽內(nèi)的文字是有顏色。使用tkinter庫實(shí)現(xiàn),并以class的形式書寫,方便用戶對內(nèi)容進(jìn)行擴(kuò)展開發(fā)。
窗口默認(rèn)出現(xiàn)在屏幕的中間位置。窗口中的標(biāo)簽需要包含兩項(xiàng)內(nèi)容。其中一項(xiàng)用于實(shí)時(shí)顯示當(dāng)前的日期和時(shí)間,精確到毫秒。另一項(xiàng)從txt文件中讀取顯示,若沒有txt文件則顯示“None”。
在未鎖定狀態(tài)下,鼠標(biāo)可以拖動窗口。在鎖定狀態(tài)下,窗口無法通過鼠標(biāo)的拖動而移動。在窗口中添加一個(gè)“鎖定”按鈕,當(dāng)鼠標(biāo)移動到窗口上方時(shí),顯示“鎖定”按鈕,鼠標(biāo)移走后,隱藏“鎖定”按鈕。通過“鎖定”按鈕,窗口進(jìn)入鎖定狀態(tài)。在鎖定狀態(tài)下,當(dāng)鼠標(biāo)移動到窗口上方時(shí),顯示一個(gè)“解除鎖定”的按鈕,鼠標(biāo)移走后,隱藏該“解除鎖定”按鈕。通過點(diǎn)擊“解除鎖定”按鈕,進(jìn)入未鎖定狀態(tài)。鎖定和未鎖定狀態(tài)是互相切換的。
給窗口添加一個(gè)鼠標(biāo)右鍵的功能,在右鍵菜單中,可以點(diǎn)擊“退出”,從而退出應(yīng)用。
窗口中的內(nèi)容居中顯示。
代碼
給出的代碼,并經(jīng)過微調(diào):
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) # 設(shè)置窗口透明度 # self.attributes('-topmost', True) # 窗口置頂 # self.attributes('-transparentcolor', '#000000') self.overrideredirect(True) # 去掉窗口邊框 self.locked = False # 初始化鎖定狀態(tài) 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)) # 添加日期時(shí)間標(biāo)簽 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') # 提示標(biāo)簽 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') # 文本標(biāo)簽 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) # 定時(shí)更新日期時(shí)間標(biāo)簽 self.update_datetime() # 定時(shí)更新text標(biāo)簽 self.update_text_label() # 定時(shí)更新note標(biāo)簽 self.update_note_label() # 綁定鼠標(biāo)事件 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) # 創(chuàng)建右鍵菜單 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 = '小鋒學(xué)長生活大爆炸' 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() # 計(jì)算距離指定日期過了多少周 start_delta = today - specified_start_date num_of_weeks = math.ceil(start_delta.days / 7) # 計(jì)算距離指定日期剩余多少周 end_delta = specified_end_date - today remain_weeks = math.ceil(end_delta.days / 7) msg = f'當(dāng)前第{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()
效果
說明
關(guān)于背景顏色、日期等等內(nèi)容,大家可以修改相應(yīng)的代碼。
到此這篇關(guān)于Python調(diào)用ChatGPT制作基于Tkinter的桌面時(shí)鐘的文章就介紹到這了,更多相關(guān)Python ChatGPT制作桌面時(shí)鐘內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
10 行 Python 代碼教你自動發(fā)送短信(不想回復(fù)工作郵件妙招)
這篇文章主要介紹了10 行 Python 代碼教你自動發(fā)送短信(不想回復(fù)工作郵件妙招),目前在國內(nèi)通過手機(jī)短信保障信息安全是比較常見的,具體實(shí)例代碼大家跟隨小編一起通過本文學(xué)習(xí)吧2018-10-10Pytorch中Tensor與各種圖像格式的相互轉(zhuǎn)化詳解
這篇文章主要介紹了Pytorch中Tensor與各種圖像格式的相互轉(zhuǎn)化詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12Python實(shí)現(xiàn)全局變量的兩個(gè)解決方法
這篇文章主要介紹了Python實(shí)現(xiàn)全局變量的兩個(gè)解決方法,需要的朋友可以參考下2014-07-07Pandas DataFrame轉(zhuǎn)換為字典的方法
實(shí)際開發(fā)中我們可能會遇到一類問題,如何將Pandas DataFrame轉(zhuǎn)換為字典,本文就來介紹一下,感興趣的可以了解一下2021-05-05Python中的defaultdict與__missing__()使用介紹
下面這篇文章主要給大家介紹了關(guān)于Python中defaultdict使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-02-02python matplotlib畫圖實(shí)例代碼分享
這篇文章主要介紹了python matplotlib畫圖實(shí)例代碼分享,具有一定借鑒價(jià)值,需要的朋友可以參考下2017-12-12