py
import time
import tkinter as tk
import threading
class FloatingWindow(tk.Toplevel):
def __init__(self, master=None, **kwargs):
super().__init__(master, **kwargs)
self.overrideredirect(True) # 隐藏标题栏和边框
self.attributes("-alpha", 0.8) # 设置窗口透明度
self.attributes("-topmost", True) # 将窗口设置为最上层
# 添加文本
self.label = tk.Label(self, text="123")
self.label.pack()
# 绑定鼠标移动事件
self.bind("<B1-Motion>", self.move_window)
def move_window(self, event):
self.geometry(f"+{event.x_root}+{event.y_root}")
def change_text(self, new_text):
self.label.config(text=new_text)
count = 0
floating_window = None
def run_floating_window():
global floating_window
root = tk.Tk()
root.withdraw() # 隐藏主窗口
floating_window = FloatingWindow(root)
floating_window.mainloop()
thread = threading.Thread(target=run_floating_window)
thread.start()
while 1:
if floating_window is not None:
floating_window.change_text(f"Count: {count}") # 调用方法改变文本
time.sleep(1)
count += 1