我需要在PyQt5上实现画笔的帮助 我已经有一些鼠标事件代码:
def mousePressEvent(self, event):
if event.button() and event.button() == Qt.LeftButton:
self.lastPoint = event.pos()
self.scribbling = True
def mouseMoveEvent(self, event):
if (event.buttons() & Qt.LeftButton) and self.scribbling:
self.drawLineTo(event.pos())
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton and self.scribbling:
self.drawLineTo(event.pos())
self.scribbling = False
在其中声明了画笔绘制功能的
def drawLineTo(self, endPoint):
painter = QPainter(self.image)
painter.setPen(QPen(self.myPenColor, self.myPenWidth, Qt.SolidLine,
Qt.RoundCap, Qt.RoundJoin))
painter.drawLine(self.lastPoint, endPoint)
self.modified = True
rad = self.myPenWidth / 2 + 2
self.update(QRect(self.lastPoint, endPoint).normalized().adjusted(-rad, -rad, +rad, +rad))
self.lastPoint = QPoint(endPoint)
但是主要的问题是此函数在事件本身中声明。因此,绘图会立即进行,并且我无法添加其他工具。因为与他们一起不断绘制“铅笔”。但是我需要以某种方式从那里拉出功能并将其分配给相应的按钮。仅通过单击此按钮即可包括在内。 假设我有一些工具栏:
toolbar = self.addToolBar('Инструменты')
toolbar.addAction(self.pen)
对此有操作:
self.pen = QAction(QIcon('Image/pen.png'), 'Pencil', self)
self.pen.triggered.connect(self. )
我将如何在“ triggered.connect”中分配绘图功能,并且该功能仅在单击按钮时才有效。 也许有一些类似之处,例如在tkinter中:
def draw_pen(self):
self.parent.config(cursor="arrow")
self.parent.unbind("<Button-1>")
self.parent.unbind("<Motion>")
self.parent.bind("<ButtonPress-1>", self.button_press)
self.parent.bind('<B1-Motion>', self.draw_pencil_move)
self.parent.bind('<ButtonRelease-1>', self.draw_pencil_release)
最后,我只是将此功能分配给了按钮,一切正常
我将非常感谢您的回答,特别是对于解决问题的示例或事件中未声明的画笔的示例
P.S。如果出现问题,我为我的英语致歉с:
答案 0 :(得分:0)
尝试一下:
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class MyScribbling(QMainWindow):
def __init__(self):
super().__init__()
self.penOn = QAction(QIcon('Image/ok.png'), 'Включить рисование', self)
self.penOn.triggered.connect(self.drawingOn)
self.penOff = QAction(QIcon('Image/exit.png'), 'ВЫКЛЮЧИТЬ рисование', self)
self.penOff.triggered.connect(self.drawingOff)
toolbar = self.addToolBar('Инструменты')
toolbar.addAction(self.penOn)
toolbar.addAction(self.penOff)
self.scribbling = False
self.myPenColor = Qt.red # +
self.myPenWidth = 3 # +
self.lastPoint = QPoint()
self.image = QPixmap("Image/picture.png")
self.setFixedSize(600, 600)
self.resize(self.image.width(), self.image.height())
self.show()
# +++
def paintEvent(self, event):
painter = QPainter(self)
painter.drawPixmap(self.rect(), self.image)
def mousePressEvent(self, event):
# if event.button() and event.button() == Qt.LeftButton: # -
if (event.button() == Qt.LeftButton) and self.scribbling: # +
self.lastPoint = event.pos()
# self.scribbling = True # -
def mouseMoveEvent(self, event):
if (event.buttons() & Qt.LeftButton) and self.scribbling:
# self.drawLineTo(event.pos()) # -
# +++
painter = QPainter(self.image)
painter.setPen(QPen(self.myPenColor, self.myPenWidth,
Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
painter.drawLine(self.lastPoint, event.pos())
# self.modified = True # ?
self.lastPoint = event.pos()
self.update()
# ?
#rad = self.myPenWidth / 2 + 2
#self.update(QRect(self.lastPoint, event.pos()).normalized().adjusted(-rad, -rad, +rad, +rad))
#self.lastPoint = QPoint(event.pos())
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton and self.scribbling:
#self.drawLineTo(event.pos())
#self.scribbling = False
pass
# Перенес в mouseMoveEvent
# def drawLineTo(self, endPoint):
# painter = QPainter(self.image)
# painter.setPen(QPen(self.myPenColor, self.myPenWidth, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
# painter.drawLine(self.lastPoint, endPoint)
# self.modified = True
# rad = self.myPenWidth / 2 + 2
# self.update(QRect(self.lastPoint, endPoint).normalized().adjusted(-rad, -rad, +rad, +rad))
# self.lastPoint = QPoint(endPoint)
# +++
def drawingOn(self):
self.scribbling = True
# +++
def drawingOff(self):
self.scribbling = False
if __name__ == '__main__':
app = QApplication(sys.argv)
mainMenu = MyScribbling()
sys.exit(app.exec_())