python在鼠标上悬挂线条

用python在鼠标上悬挂一根横着的红线,不影响点击和正常使用,想了很多办法都无法实现,求指点!
目前思路:

  1. 更改鼠标样式(影响正常使用)
import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QPixmap, QCursor
from PyQt5.QtCore import Qt

if __name__ == "__main__":
    app = QApplication(sys.argv)

    # 加载自己的图片
    pixmap = QPixmap("custom_cursor.png")

    # 创建一个QCursor对象并设置自定义鼠标形状
    cursor = QCursor(pixmap)

    # 设置应用程序全局的鼠标形状为自定义形状
    app.setOverrideCursor(cursor)

    # 创建一个QWidget窗口并显示
    widget = QWidget()
    widget.setGeometry(0, 0, 1919, 1079)
    widget.show()

    # 设置窗口为透明
    # widget.setAttribute(Qt.WA_TranslucentBackground)
    # widget.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
    # 设置窗口的透明度
    widget.setWindowOpacity(0.05)  # 设置透明度为0.5(范围为010代表完全透明,1代表完全不透明)

    sys.exit(app.exec_())


  1. 制作背景透明的窗口(不是很行)
"""
@Author: ChatGPT
"""

from PIL import Image, ImageDraw
import sys
from PyQt5.QtWidgets import QApplication, QLabel
from PyQt5.QtGui import QImage, QPixmap, QCursor
from PyQt5.QtCore import Qt, QTimer, QEventLoop


class CustomWindow(QLabel):
    def __init__(self):
        super().__init__()
        self.setGeometry(0, 0, 1919, 1079)
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)

        # 创建一个RGBA模式的空白图像
        self.image = Image.new('RGBA', (1918, 1078))
        self.painter = ImageDraw.Draw(self.image)

        # 定时器用于更新窗口位置
        self.timer = QTimer()
        self.timer.timeout.connect(self.update_position)
        self.timer.start(10)

        self.move_with_mouse()  # 移动窗口到鼠标初始位置
        self.draw_line(1800, 0, 0, 0, 255, 150)
        self.draw_line(500, 500, 0, 255, 0, 180)
        # self.draw_line(500, -500, 0, 255, 0, 180)

        # 将图像转换为QImage并显示在窗口上
        qimage = QImage(self.image.tobytes(), self.image.size[0], self.image.size[1], QImage.Format_ARGB32)
        pixmap = QPixmap.fromImage(qimage)
        self.setPixmap(pixmap)

    def move_with_mouse(self):
        # 将窗口移动到鼠标当前位置

        # 创建一个事件循环,用于延迟执行
        loop = QEventLoop()

        # 使用定时器触发事件循环退出,延迟一小段时间
        QTimer.singleShot(1, loop.quit)

        # 进入事件循环,等待定时器触发退出
        loop.exec_()

        # 获取鼠标位置并移动窗口
        self.move(QCursor.pos())

    def update_position(self):
        # 更新窗口位置为鼠标位置
        self.move_with_mouse()

    def draw_line(self, a, b, c, d, e, f):
        # 获取鼠标位置
        mouse_pos = QCursor.pos()

        # 调整线段的起点坐标使其与鼠标位置重合
        start_x = mouse_pos.x() - 20
        start_y = mouse_pos.y() - 825

        # 在图像上绘制不透明的线条
        self.painter.line((start_x, start_y, start_x + a, start_y + b), fill=(c, d, e, f), width=5)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = CustomWindow()
    window.show()
    sys.exit(app.exec_())


代码基本上是ChatGPT写的,被我大幅度改过了。

import tkinter as tk
from PIL import Image, ImageTk

root = tk.Tk()
root.overrideredirect(True) # 隐藏标题栏、窗口在任务栏上的图标

root.attributes("-transparentcolor", "white") # 设置白色为透明色
root.attributes("-topmost", True) # 置顶窗口

image = ImageTk.PhotoImage(Image.open("custom_cursor.png"))
root.geometry("%dx%d"%(image.width(), image.height())) # 更改窗口大小
tk.Label(root, image=image, bg="white").pack() # 显示光标装饰

def update_pos():
    x, y = root.winfo_pointerxy() # 获取光标位置
    x -= image.width() // 2
    y += 2
    
    root.geometry("+%d+%d"%(x, y)) # 更改窗口位置
    
while True:
    update_pos()
    root.update()

可正常运行

看看这个能不能实现:

import pyautogui
import time

# 获取屏幕大小
screenWidth, screenHeight = pyautogui.size()

# 创建红色线条
的像素数组
redLine = [(0, 0, 255), (0, screenHeight - 1, 255), (screenWidth - 1, 0, 255), (screenWidth - 1, screenHeight - 1, 255)]

# 在鼠标上悬挂红线
while True:
    for x in range(len(redLine)):
        pyautogui.draw(redLine[x])
        pyautogui.moveTo(redLine[x][0], redLine[x][1])
        time.sleep(0.1)
    pyautogui.mouseDown()
    pyautogui.moveRel(0.5, 0.5)
    pyautogui.mouseUp()
    time.sleep(0.1)