【发布时间】:2018-09-21 21:51:55
【问题描述】:
我需要关于在 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)
最后,我只是将此功能分配给按钮,一切正常
我将非常感谢您的回答,尤其是解决问题的示例或未在事件中声明的画笔示例
附:如果有问题,我为我的英语道歉с:
【问题讨论】:
标签: python user-interface drawing pyqt5 draw