Q:

Writing text in multiline in python

I am working on a text editor. The only challenge left for me is to write text in next line when text width(written) exceeded from its maximum size(window).
Any help will be appreciated. I have a class photo viewer that controls text font, size etc.Thanks in advance
class PhotoViewer(QtWidgets.QGraphicsView):
    photoClicked = QtCore.pyqtSignal(QtCore.QPoint)

    def __init__(self, parent):
        super(PhotoViewer, self).__init__(parent)
        self._zoom = 0
        self._empty = True
        self._scene = QtWidgets.QGraphicsScene(self)
        self._photo = QtWidgets.QGraphicsPixmapItem()

        self._textLayer = QtWidgets.QGraphicsSimpleTextItem ()

        self._scene.addItem(self._photo)
        self._scene.addItem(self._textLayer)

        self.setScene(self._scene)
        self.setTransformationAnchor(QtWidgets.QGraphicsView.AnchorUnderMouse)
        self.setResizeAnchor(QtWidgets.QGraphicsView.AnchorUnderMouse)
        self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.setBackgroundBrush(QtGui.QBrush(QtGui.QColor(80, 30, 30)))
        self.setFrameShape(QtWidgets.QFrame.NoFrame)
        self._textLayer.setFlags(QGraphicsItem.ItemIsMovable)

    def updateText(self,text,font_size=50):
        # Load the font:
        font_db = QFontDatabase()
        font_id = font_db.addApplicationFont("fonts/Summer's Victory Over Spring - TTF.ttf")
        #families = font_db.applicationFontFamilies(font_id)
        #print (families)

        myFont = QFont("Summers Victory Over Spring")
        myFont.setPixelSize(font_size*1.5)
        self._textLayer.setFont(myFont)
        self._textLayer.setText(text)

class Window(QtWidgets.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.viewer = PhotoViewer(self)
        # 'Load image' button

        # Button to change from drag/pan to getting pixel info
        self.btnPixInfo = QtWidgets.QToolButton(self)
        self.btnPixInfo.setText('Create Text')
        self.btnPixInfo.clicked.connect(self.loadText)

        self.fontSize =QtWidgets.QSpinBox()
        self.fontSize.valueChanged.connect(self.loadText)

        self.editPixInfo = QtWidgets.QLineEdit(self)
        #self.editPixInfo.setReadOnly(True)
        self.viewer.photoClicked.connect(self.photoClicked)
        # Arrange layout
        VBlayout = QtWidgets.QVBoxLayout(self)
        HBlayout = QtWidgets.QHBoxLayout()
        HBlayout.setAlignment(QtCore.Qt.AlignLeft)

        HBlayout.addWidget(self.btnPixInfo)
        HBlayout.addWidget(self.editPixInfo)
        VBlayout.addLayout(HBlayout)
        VBlayout.addWidget(self.viewer)
        HBlayout.addWidget(self.fontSize)
        self.editPixInfo.setText("Sheeda")
        self.fontSize.setValue(20)

        self.loadText()
        self.frame = QFrame()
        self.frame.setFrameStyle(QFrame.StyledPanel)
        self.frame.setLineWidth(20)

    def loadText(self):
        #self.viewer.toggleDragMode()
        self.viewer.updateText(self.editPixInfo.text(),self.fontSize.value())

    def photoClicked(self, pos):
        if self.viewer.dragMode()  == QtWidgets.QGraphicsView.NoDrag:
            self.editPixInfo.setText('%d, %d' % (pos.x(), pos.y()))

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    window.setGeometry(500, 300, 800, 600)
    window.show()
    sys.exit(app.exec_())

Here is my code so far.

A:

I see, you are using QGraphicsSimpleTextItem where as your easiest bet is to use QGraphicsTextItem. Reason being, later offers a setTextWidth method,defauls to -1. Just set this to your 'maximumSize' limit. also,you have to update your "setText" call to "setPlainText" call as per QGraphicsTextItem Docs.

