如何在python pyqt5中制作透明十字符号
How to make transparent cross symbol in python pyqt5
有谁知道如何让我的十字准线透明或不透明?
我正在尝试制作如下所示的十字准线:
代码如下:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Crosshair(QtWidgets.QWidget):
def __init__(self, parent=None, windowSize=24, penWidth=2):
QtWidgets.QWidget.__init__(self, parent)
self.ws = windowSize
self.resize(windowSize+1, windowSize+1)
self.pen = QtGui.QPen(QtGui.QColor(0,255,0,255))
self.pen.setWidth(penWidth)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.WindowStaysOnTopHint | QtCore.Qt.WindowTransparentForInput)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground, True)
self.move(QtWidgets.QApplication.desktop().screen().rect().center() - self.rect().center() + QtCore.QPoint(1,1))
def paintEvent(self, event):
ws = self.ws
d = 241
painter = QtGui.QPainter(self)
painter.setPen(self.pen)
# painter.drawLine( x1,y1, x2,y2 )
painter.drawLine(ws/2, 0, ws/2, ws/2 - ws/d) # Top
painter.drawLine(ws/2, ws/2 + ws/d, ws/2, ws) # Bottom
painter.drawLine(0, ws/2, ws/2 - ws/d, ws/2) # Left
painter.drawLine(ws/2 + ws/d, ws/2, ws, ws/2) # Right
app = QtWidgets.QApplication(sys.argv)
widget = Crosshair(windowSize=241, penWidth=2.5)
widget.show()
sys.exit(app.exec_())
您可以使用 QPainter 中的 setOpacity()
功能。
You can see the documentation here
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Crosshair(QtWidgets.QWidget):
def __init__(self, parent=None, windowSize=24, penWidth=2, opacity=1):
QtWidgets.QWidget.__init__(self, parent)
self.ws = windowSize
self.resize(windowSize+1, windowSize+1)
self.pen = QtGui.QPen(QtGui.QColor(0,255,0,255))
self.pen.setWidth(penWidth)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.WindowStaysOnTopHint | QtCore.Qt.WindowTransparentForInput)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground, True)
self.move(QtWidgets.QApplication.desktop().screen().rect().center() - self.rect().center() + QtCore.QPoint(1,1))
self.opacity = opacity
def paintEvent(self, event):
ws = self.ws
d = 241
painter = QtGui.QPainter(self)
painter.setOpacity(self.opacity)
painter.setPen(self.pen)
# painter.drawLine( x1,y1, x2,y2 )
painter.drawLine(ws/2, 0, ws/2, ws/2 - ws/d) # Top
painter.drawLine(ws/2, ws/2 + ws/d, ws/2, ws) # Bottom
painter.drawLine(0, ws/2, ws/2 - ws/d, ws/2) # Left
painter.drawLine(ws/2 + ws/d, ws/2, ws, ws/2) # Right
app = QtWidgets.QApplication(sys.argv)
widget = Crosshair(windowSize=241, penWidth=2.5, opacity=0.5)
widget.show()
sys.exit(app.exec_())
希望对您有所帮助:)
有谁知道如何让我的十字准线透明或不透明?
我正在尝试制作如下所示的十字准线:
代码如下:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Crosshair(QtWidgets.QWidget):
def __init__(self, parent=None, windowSize=24, penWidth=2):
QtWidgets.QWidget.__init__(self, parent)
self.ws = windowSize
self.resize(windowSize+1, windowSize+1)
self.pen = QtGui.QPen(QtGui.QColor(0,255,0,255))
self.pen.setWidth(penWidth)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.WindowStaysOnTopHint | QtCore.Qt.WindowTransparentForInput)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground, True)
self.move(QtWidgets.QApplication.desktop().screen().rect().center() - self.rect().center() + QtCore.QPoint(1,1))
def paintEvent(self, event):
ws = self.ws
d = 241
painter = QtGui.QPainter(self)
painter.setPen(self.pen)
# painter.drawLine( x1,y1, x2,y2 )
painter.drawLine(ws/2, 0, ws/2, ws/2 - ws/d) # Top
painter.drawLine(ws/2, ws/2 + ws/d, ws/2, ws) # Bottom
painter.drawLine(0, ws/2, ws/2 - ws/d, ws/2) # Left
painter.drawLine(ws/2 + ws/d, ws/2, ws, ws/2) # Right
app = QtWidgets.QApplication(sys.argv)
widget = Crosshair(windowSize=241, penWidth=2.5)
widget.show()
sys.exit(app.exec_())
您可以使用 QPainter 中的 setOpacity()
功能。
You can see the documentation here
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Crosshair(QtWidgets.QWidget):
def __init__(self, parent=None, windowSize=24, penWidth=2, opacity=1):
QtWidgets.QWidget.__init__(self, parent)
self.ws = windowSize
self.resize(windowSize+1, windowSize+1)
self.pen = QtGui.QPen(QtGui.QColor(0,255,0,255))
self.pen.setWidth(penWidth)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.WindowStaysOnTopHint | QtCore.Qt.WindowTransparentForInput)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground, True)
self.move(QtWidgets.QApplication.desktop().screen().rect().center() - self.rect().center() + QtCore.QPoint(1,1))
self.opacity = opacity
def paintEvent(self, event):
ws = self.ws
d = 241
painter = QtGui.QPainter(self)
painter.setOpacity(self.opacity)
painter.setPen(self.pen)
# painter.drawLine( x1,y1, x2,y2 )
painter.drawLine(ws/2, 0, ws/2, ws/2 - ws/d) # Top
painter.drawLine(ws/2, ws/2 + ws/d, ws/2, ws) # Bottom
painter.drawLine(0, ws/2, ws/2 - ws/d, ws/2) # Left
painter.drawLine(ws/2 + ws/d, ws/2, ws, ws/2) # Right
app = QtWidgets.QApplication(sys.argv)
widget = Crosshair(windowSize=241, penWidth=2.5, opacity=0.5)
widget.show()
sys.exit(app.exec_())
希望对您有所帮助:)