问题描述
我需要关于在 pyqt5 上实现画笔的帮助我已经有了一些鼠标事件代码:
i need help with the implementation of the brush on pyqt5 i already have some event code for the mouse:
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
其中声明了画笔绘制函数:
inside of which the brush drawing function is declared:
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)
但主要问题是这个函数是在事件本身中声明的.因此,绘图立即进行,我无法添加其他工具.因为与他们一起,铅笔"将不断被绘制.但是我需要以某种方式从那里拉出该功能并将其分配给相应的按钮.仅通过单击此按钮来包含.假设我有一些工具栏:
but the main problem is that this function is declared in the events themselves. therefore, drawing goes immediately, and i can not add other tools. because together with them the "pencil" will constantly be drawn. but i need to somehow pull out the function from there and assign it to the corresponding button. to include only by clicking on this button. let's say i have some toolbar:
toolbar = self.addtoolbar('инструменты')
toolbar.addaction(self.pen)
有一个动作:
self.pen = qaction(qicon('image/pen.png'), 'pencil', self)
self.pen.triggered.connect(self. )
我将如何在triggered.connect"中分配绘图功能,并且它仅在单击按钮时才起作用.也许这有一些联系,比如在 tkinter 中,在相似性中:
how would i do so in the "triggered.connect" assign the drawing function, and that it works only when the button is clicked. maybe there are some bonds for this, like in tkinter, in the similarity:
def draw_pen(self):
self.parent.config(cursor="arrow")
self.parent.unbind("")
self.parent.unbind("")
self.parent.bind("", self.button_press)
self.parent.bind('', self.draw_pencil_move)
self.parent.bind('', self.draw_pencil_release)
最后,我只是将此功能分配给按钮,一切正常
and, in the end, i just assigned this function to the button, and everything worked fine
我将非常感谢您的回答,尤其是解决问题的示例或未在事件中声明的画笔示例
i will be very grateful for answers and especially for examples of solving problems or examples of brushes that are not declared in the events
附:如果有问题,我为我的英语道歉с:
p.s. i apologize for my english, if something is wrong с:
推荐答案
试试看:
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_())
灯光没o耀眼