用python在鼠标上悬挂一根横着的红线,不影响点击和正常使用,想了很多办法都无法实现,求指点!
目前思路:
- 更改鼠标样式(影响正常使用)
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(范围为0到1,0代表完全透明,1代表完全不透明)
sys.exit(app.exec_())
- 制作背景透明的窗口(不是很行)
"""
@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写的,被我大幅度改过了。