[Python] 纯文本查看 复制代码
import tkinter as tk
import ctypes
from ctypes import wintypes
import threading
import time
class StatusWindow:
def __init__(self):
self.window = tk.Tk()
self.window.overrideredirect(True)
self.window.attributes('-topmost', True)
self.window.attributes('-transparentcolor', 'magenta')
self.window.configure(bg='magenta')
# 你的原始参数
self.window_size = 40
self.window.geometry(f'{self.window_size}x{self.window_size}+0+0')
self.window.resizable(False, False)
# 背景色
self.bg_color = '#E6F0FF'
# 创建画布
self.canvas = tk.Canvas(
self.window,
width=self.window_size,
height=self.window_size,
bg=self.bg_color,
highlightthickness=0,
bd=0
)
self.canvas.pack()
self.canvas.pack_propagate(False)
# 你的字体参数
self.chinese_font = ('楷体', 26, 'bold')
self.english_font = ('KaiTi', 28, 'bold')
# 创建文字
self.text_id = self.canvas.create_text(
self.window_size//2,
self.window_size//2,
text='中',
fill='#4CAF50',
font=self.chinese_font,
anchor='center'
)
# 状态变量
self.current_text = '中'
self.current_color = '#4CAF50'
self.current_font = self.chinese_font
self.running = True
self.last_x = 0
self.last_y = 0
self.last_caps = 0
self.last_ime = None
# Windows API
self.user32 = ctypes.windll.user32
self.imm32 = ctypes.windll.imm32
# 设置透明度
self.set_transparency(180)
# 启动
self.start()
def set_transparency(self, alpha):
"""设置透明度"""
try:
hwnd = self.user32.GetParent(self.window.winfo_id())
style = self.user32.GetWindowLongW(hwnd, -20)
self.user32.SetWindowLongW(hwnd, -20, style | 0x80000)
self.user32.SetLayeredWindowAttributes(hwnd, 0, alpha, 0x00000002)
except:
pass
def get_input_status(self):
"""获取输入法状态"""
try:
hwnd = self.user32.GetForegroundWindow()
hIME = self.imm32.ImmGetDefaultIMEWnd(hwnd)
if hIME:
ime_status = self.user32.SendMessageW(hIME, 0x0283, 0x0005, 0)
else:
ime_status = 0
caps_status = self.user32.GetKeyState(0x14) & 1
return ime_status, caps_status
except:
return 0, 0
def update_display(self):
"""更新显示"""
ime, caps = self.get_input_status()
if caps:
display_text = 'A'
text_color = '#FF9800'
font = self.english_font
else:
if ime == 1:
display_text = '中'
text_color = '#4CAF50'
font = self.chinese_font
else:
display_text = '英'
text_color = '#2196F3'
font = self.english_font
if (display_text != self.current_text or
text_color != self.current_color or
font != self.current_font):
self.canvas.itemconfig(
self.text_id,
text=display_text,
fill=text_color,
font=font
)
self.current_text = display_text
self.current_color = text_color
self.current_font = font
def key_monitor(self):
"""按键检测 - 立即响应 (1ms轮询)"""
ime, self.last_caps = self.get_input_status()
self.last_ime = ime
while self.running:
try:
ime, caps = self.get_input_status()
if caps != self.last_caps or ime != self.last_ime:
self.window.after(1, self.update_display)
self.last_caps = caps
self.last_ime = ime
time.sleep(0.001) # 1ms轮询
except:
time.sleep(0.01)
def timer_monitor(self):
"""定时检测 - 每秒更新一次(确保同步)"""
while self.running:
try:
time.sleep(1) # 每秒检测一次
self.window.after(1, self.update_display)
except:
time.sleep(1)
def follow_mouse(self):
"""鼠标跟随 - 无延迟"""
class POINT(ctypes.Structure):
_fields_ = [("x", ctypes.c_long), ("y", ctypes.c_long)]
point = POINT()
while self.running:
try:
if self.user32.GetCursorPos(ctypes.byref(point)):
x = point.x + 25
y = point.y + 20
self.window.geometry(f'+{x}+{y}')
time.sleep(0.001)
except:
time.sleep(0.01)
def topmost_enhancer(self):
"""增强置顶效果:每0.5秒提升窗口一次,确保不被其他置顶窗口遮挡"""
while self.running:
try:
time.sleep(0.5)
self.window.after(0, self.window.lift)
except:
time.sleep(0.5)
def start(self):
"""启动所有检测"""
self.update_display()
# 按键检测线程 (1ms)
threading.Thread(target=self.key_monitor, daemon=True).start()
# 定时检测线程 (1秒)
threading.Thread(target=self.timer_monitor, daemon=True).start()
# 鼠标跟随线程
threading.Thread(target=self.follow_mouse, daemon=True).start()
# 新增:置顶增强线程
threading.Thread(target=self.topmost_enhancer, daemon=True).start()
def run(self):
"""运行"""
try:
self.window.mainloop()
except KeyboardInterrupt:
self.running = False
self.window.destroy()
if __name__ == '__main__':
# 进程守护:自动重启
while True:
try:
app = StatusWindow()
app.run()
except Exception as e:
print(f"程序异常退出: {e},5秒后重启...")
time.sleep(5)
except KeyboardInterrupt:
print("用户中断,退出程序")
break
finally:
# 确保旧实例的线程能退出
try:
app.running = False
except:
pass
time.sleep(1)