【问题标题】:Drawing a line consisting of multiple points using PyQt使用 PyQt 绘制由多个点组成的线
【发布时间】:2017-01-28 02:28:48
【问题描述】:

我想使用 PyQt 在 Python 脚本中通过鼠标单击绘制一条由多个点组成的线。我需要所有的 ponts 坐标,并且我希望能够删除这条线。这是我的脚本完成所有工作,除了图形线条图本身,它只是打印它所做的:

#!/usr/bin/python3

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class endomess(QMainWindow):

    def __init__(self):
        QMainWindow.__init__(self)
        self.draw = False

    def mousePressEvent(self, event):

        if event.button() == Qt.LeftButton:

            if self.draw == False:
                print('Starting to draw at', str(event.pos()))
                self.draw = True
                self.linePoints = []

            elif self.draw == True:
                print('Appending', str(event.pos()))

            self.linePoints.append(event.pos())

        elif event.button() == Qt.RightButton:
            if self.draw == True:
                print('Finished drawing. List of all points:', str(self.linePoints))
                self.draw = False

def main(argv):
    app = QApplication(argv, True)
    wnd = endomess()
    wnd.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main(sys.argv)

所以,我的问题是:我如何实际绘制可以通过上述脚本定义的线?我已经看过 scribble.py 和一些 Qt 绘画文档,但我不明白。对 Qt 经验丰富的人来说,这可能不是问题?

提前感谢所有帮助!

【问题讨论】:

    标签: python pyqt drawing pyqt4


    【解决方案1】:

    您可能应该使用graphics view framework 来绘制线条,而不是尝试直接绘制它们。

    这是一个基本的演示,可帮助您入门:

    from PyQt4 import QtGui, QtCore
    
    class Window(QtGui.QWidget):
        def __init__(self):
            QtGui.QWidget.__init__(self)
            self.view = View(self)
            self.button = QtGui.QPushButton('Clear View', self)
            self.button.clicked.connect(self.handleClearView)
            layout = QtGui.QVBoxLayout(self)
            layout.addWidget(self.view)
            layout.addWidget(self.button)
    
        def handleClearView(self):
            self.view.scene().clear()
    
    class View(QtGui.QGraphicsView):
        def __init__(self, parent):
            QtGui.QGraphicsView.__init__(self, parent)
            self.setScene(QtGui.QGraphicsScene(self))
            self.setSceneRect(QtCore.QRectF(self.viewport().rect()))
    
        def mousePressEvent(self, event):
            self._start = event.pos()
    
        def mouseReleaseEvent(self, event):
            start = QtCore.QPointF(self.mapToScene(self._start))
            end = QtCore.QPointF(self.mapToScene(event.pos()))
            self.scene().addItem(
                QtGui.QGraphicsLineItem(QtCore.QLineF(start, end)))
            for point in (start, end):
                text = self.scene().addSimpleText(
                    '(%d, %d)' % (point.x(), point.y()))
                text.setBrush(QtCore.Qt.red)
                text.setPos(point)
    
    if __name__ == '__main__':
    
        import sys
        app = QtGui.QApplication(sys.argv)
        window = Window()
        window.resize(640, 480)
        window.show()
        sys.exit(app.exec_())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-18
      • 1970-01-01
      • 1970-01-01
      • 2019-03-14
      • 1970-01-01
      • 1970-01-01
      • 2021-04-26
      • 2020-11-10
      相关资源
      最近更新 更多