溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python怎么調(diào)用ChatGPT制作基于Tkinter的桌面時鐘

發(fā)布時間:2023-04-13 09:46:23 來源:億速云 閱讀:127 作者:iii 欄目:開發(fā)技術(shù)

本文小編為大家詳細(xì)介紹“Python怎么調(diào)用ChatGPT制作基于Tkinter的桌面時鐘”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“Python怎么調(diào)用ChatGPT制作基于Tkinter的桌面時鐘”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識吧。

描述

給ChatGPT的描述內(nèi)容:

python在桌面上顯示動態(tài)的文字,不要顯示窗口邊框。窗口背景和標(biāo)簽背景都是透明的,但標(biāo)簽內(nèi)的文字是有顏色。使用tkinter庫實現(xiàn),并以class的形式書寫,方便用戶對內(nèi)容進(jìn)行擴(kuò)展開發(fā)。

窗口默認(rèn)出現(xiàn)在屏幕的中間位置。窗口中的標(biāo)簽需要包含兩項內(nèi)容。其中一項用于實時顯示當(dāng)前的日期和時間,精確到毫秒。另一項從txt文件中讀取顯示,若沒有txt文件則顯示“None”。

在未鎖定狀態(tài)下,鼠標(biāo)可以拖動窗口。在鎖定狀態(tài)下,窗口無法通過鼠標(biāo)的拖動而移動。在窗口中添加一個“鎖定”按鈕,當(dāng)鼠標(biāo)移動到窗口上方時,顯示“鎖定”按鈕,鼠標(biāo)移走后,隱藏“鎖定”按鈕。通過“鎖定”按鈕,窗口進(jìn)入鎖定狀態(tài)。在鎖定狀態(tài)下,當(dāng)鼠標(biāo)移動到窗口上方時,顯示一個“解除鎖定”的按鈕,鼠標(biāo)移走后,隱藏該“解除鎖定”按鈕。通過點擊“解除鎖定”按鈕,進(jìn)入未鎖定狀態(tài)。鎖定和未鎖定狀態(tài)是互相切換的。

給窗口添加一個鼠標(biāo)右鍵的功能,在右鍵菜單中,可以點擊“退出”,從而退出應(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))
 
        # 添加日期時間標(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)
 
        # 定時更新日期時間標(biāo)簽
        self.update_datetime()
        # 定時更新text標(biāo)簽
        self.update_text_label()
        # 定時更新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()
        # 計算距離指定日期過了多少周
        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'當(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()

讀到這里,這篇“Python怎么調(diào)用ChatGPT制作基于Tkinter的桌面時鐘”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI