備忘錄_20160105(定位) 修改 回首頁

程式 2025-01-02 11:48:00 1735789680 100
python 全螢幕,最上層視窗,攔截按鍵

python 全螢幕,最上層視窗,攔截按鍵

以下是 chatgpt 產生的程式碼,可行!

import tkinter as tk

# 創建主視窗
root = tk.Tk()

# 設置視窗為全螢幕
root.attributes('-fullscreen', True)

# 設置視窗保持在前景
root.attributes('-topmost', True)

# 標籤用於顯示按鍵訊息
label = tk.Label(root, text="按下任何按鍵以顯示它的名稱", font=("Arial", 24))
label.pack(expand=True)

# 函數:處理按鍵事件
def on_key_press(event):
    key_name = f"你按下了:{event.keysym}"
    label.config(text=key_name)

# 函數:退出全螢幕
def exit_fullscreen(event):
    root.attributes('-fullscreen', False)
    root.destroy()

# 綁定鍵盤事件
root.bind("<Key>", on_key_press)  # 攔截按鍵
root.bind("<Escape>", exit_fullscreen)  # 按下 ESC 退出全螢幕

# 開始主循環
root.mainloop()