• <small id='VMCRK'></small><noframes id='VMCRK'>

      <tfoot id='VMCRK'></tfoot>
      <i id='VMCRK'><tr id='VMCRK'><dt id='VMCRK'><q id='VMCRK'><span id='VMCRK'><b id='VMCRK'><form id='VMCRK'><ins id='VMCRK'></ins><ul id='VMCRK'></ul><sub id='VMCRK'></sub></form><legend id='VMCRK'></legend><bdo id='VMCRK'><pre id='VMCRK'><center id='VMCRK'></center></pre></bdo></b><th id='VMCRK'></th></span></q></dt></tr></i><div id='VMCRK'><tfoot id='VMCRK'></tfoot><dl id='VMCRK'><fieldset id='VMCRK'></fieldset></dl></div>
        <bdo id='VMCRK'></bdo><ul id='VMCRK'></ul>

    1. <legend id='VMCRK'><style id='VMCRK'><dir id='VMCRK'><q id='VMCRK'></q></dir></style></legend>
      1. 用画笔画画

        时间:2023-08-04
          <tfoot id='WEP85'></tfoot>

        • <legend id='WEP85'><style id='WEP85'><dir id='WEP85'><q id='WEP85'></q></dir></style></legend>

            <i id='WEP85'><tr id='WEP85'><dt id='WEP85'><q id='WEP85'><span id='WEP85'><b id='WEP85'><form id='WEP85'><ins id='WEP85'></ins><ul id='WEP85'></ul><sub id='WEP85'></sub></form><legend id='WEP85'></legend><bdo id='WEP85'><pre id='WEP85'><center id='WEP85'></center></pre></bdo></b><th id='WEP85'></th></span></q></dt></tr></i><div id='WEP85'><tfoot id='WEP85'></tfoot><dl id='WEP85'><fieldset id='WEP85'></fieldset></dl></div>

                • <bdo id='WEP85'></bdo><ul id='WEP85'></ul>

                  <small id='WEP85'></small><noframes id='WEP85'>

                    <tbody id='WEP85'></tbody>
                • 本文介绍了用画笔画画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我需要关于在 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("<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)
                  

                  最后,我只是将此功能分配给按钮,一切正常

                  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_())        
                  

                  这篇关于用画笔画画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:QLabel &amp;自动换行:如何以逗号为基础换行(与 下一篇:在子进程中运行 Pyinstaller 单个可执行输出时无法

                  相关文章

                  最新文章

                • <i id='vu6dV'><tr id='vu6dV'><dt id='vu6dV'><q id='vu6dV'><span id='vu6dV'><b id='vu6dV'><form id='vu6dV'><ins id='vu6dV'></ins><ul id='vu6dV'></ul><sub id='vu6dV'></sub></form><legend id='vu6dV'></legend><bdo id='vu6dV'><pre id='vu6dV'><center id='vu6dV'></center></pre></bdo></b><th id='vu6dV'></th></span></q></dt></tr></i><div id='vu6dV'><tfoot id='vu6dV'></tfoot><dl id='vu6dV'><fieldset id='vu6dV'></fieldset></dl></div>
                    <bdo id='vu6dV'></bdo><ul id='vu6dV'></ul>
                • <small id='vu6dV'></small><noframes id='vu6dV'>

                • <legend id='vu6dV'><style id='vu6dV'><dir id='vu6dV'><q id='vu6dV'></q></dir></style></legend>

                    <tfoot id='vu6dV'></tfoot>