【发布时间】:2020-01-29 02:45:19
【问题描述】:
我的目标是制作一个小的 PC/Windows 程序,它可以让我在屏幕上逐字绘制并将结果保存为具有透明背景的 png。像Epic Pen 或gInk 这样的软件,但我的方式。全部使用 Python 3.7 和 PyQt5。
到目前为止,我设法获得了一个功能绘图应用程序(基本上遵循this tutorial),因为我同时在学习PyQt。我设法将我的草稿保存为具有透明背景的 png。我可以使绘图板全屏无边框。
现在的问题是,我找不到使整个背景透明的方法。虽然我已经找到了使用以下方法使窗口透明且无边框的方法:
Window = Window()
Window.setStyleSheet("background:transparent;")
Window.setAttribute(Qt.WA_TranslucentBackground)
Window.setWindowFlags(Qt.FramelessWindowHint)
Window.show()
它可以工作......直到我有一个绘图区。我可以在上面画画,它会以透明背景保存,但它显示为黑色。
所以我正在寻找那个解决方案。即使没有 PyQt,我也不在乎,只要我能让我的程序正常运行即可。
这是我的代码:
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QMenuBar, QMenu, QAction, QShortcut, QFileDialog
from PyQt5.QtGui import QIcon, QImage, QPainter, QPen
from PyQt5.QtCore import Qt, QPoint
class Window(QMainWindow):
def __init__(self):
super().__init__()
top = 400
left = 400
width = 800
height = 600
icon = "icons/icon.png"
self.setWindowTitle("ScreenPen drawing board")
self.setGeometry(top, left, width, height)
self.setWindowIcon(QIcon(icon))
# ---------- sets image ----------
self.image = QImage(self.size(), QImage.Format_RGBA64)
self.image.fill(Qt.transparent)
# ---------- init drawing state ----------
self.drawing = False
self.brushSize = 2
self.brushColor = Qt.red
self.lastPoint = QPoint()
# ---------- Define Menus ----------
# mainmenu
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu("File")
toolMenu = mainMenu.addMenu("Tool")
toolColor = mainMenu.addMenu("Color")
# smenu save
saveAction = QAction(QIcon("icons/save.png"), "Save", self)
saveAction.setShortcut("Ctrl+S")
fileMenu.addAction(saveAction)
saveAction.triggered.connect(self.saveFrame)
# smenu clear frame
clearFrameAction = QAction(QIcon("icons/clear.png"), "Clear Frame", self)
clearFrameAction.setShortcut("Ctrl+Del")
fileMenu.addAction(clearFrameAction)
clearFrameAction.triggered.connect(self.clearFrame)
# smenu Tool Pen
toolPenAction = QAction(QIcon("icons/toolPen.png"), "Pen", self)
# clearAction.setShortcut("Ctrl+Del")
toolMenu.addAction(toolPenAction)
# ---------- Catch Mouse Down --------
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.drawing = True
self.lastPoint = event.pos()
# ---------- Catch Mouse Move --------
def mouseMoveEvent(self, event):
if (event.buttons() & Qt.LeftButton) & self.drawing:
painter = QPainter(self.image)
painter.setPen(QPen(self.brushColor, self.brushSize, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
painter.drawLine(self.lastPoint, event.pos())
self.lastPoint = event.pos()
self.update()
# ---------- Catch Mouse Up --------
def mouseReleaseEvent(self, event):
if event.button == Qt.LeftButton:
self.drawing = False
# ---------- Paint --------
def paintEvent(self, event):
canvasPainter = QPainter(self)
canvasPainter.drawImage(self.rect(), self.image, self.image.rect())
# ---------- Save Action ----------
def saveFrame(self):
filePath, _ = QFileDialog.getSaveFileName(self, "Save Image", "", "PNG(*.png);;JPEG(*.jpg *.jpeg);; ALL Files(*.*)")
if filePath == "":
return
self.image.save(filePath)
# ---------- Clear Frame Action ----------
def clearFrame(self):
self.image.fill(Qt.white)
self.update()
if __name__ == "__main__":
app = QApplication(sys.argv)
Window = Window()
# Window style
Window.setStyleSheet("background:transparent;")
Window.setAttribute(Qt.WA_TranslucentBackground)
# Window.setWindowFlags(Qt.FramelessWindowHint)
Window.show()
app.exec()
【问题讨论】:
-
猜想:更新窗口时是否保留透明度,或者您是否需要将背景设置为透明到某种窗口的
redraw或update方法? -
我确定这不是您想要的,但是有没有办法完全删除整个窗口而只保留绘图?从根本没有窗口开始,让程序自行运行。就像当你做检测鼠标按下的事情时,你可能只保留那部分,比如 Powerpoint/Slides 上的
bring to front -
我真的不知道,我太菜鸟了。对我来说,在我当前的代码中,构成黑色背景的“东西”似乎是绘图区域本身,因为使用完全相同的代码,如果我只是删除绘图区域,我可以拥有 100% 透明的背景。所以我想要么有办法强制qT的绘图区域不显示背景,要么我应该使用其他东西,或者......不知道。
标签: python python-3.x pyqt5 screenshot transparency