在python中以多行文字

时间:2018-07-10 11:40:46

标签: python pyqt pyqt5

我正在使用文本编辑器。给我的唯一挑战是,当文本宽度(写入)超出其最大大小(窗口)时,在下一行中写入文本。 任何帮助将不胜感激。我有一个可控制文本字体,大小等的类照片查看器。谢谢!

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

到目前为止,这是我的代码。预先感谢。

1 个答案:

答案 0 :(得分:1)

我知道,您使用的是QGraphicsSimpleTextItem,最简单的选择就是使用QGraphicsTextItem。原因是,以后提供了setTextWidth方法,默认值为-1。只需将其设置为您的“ maximumSize”限制即可。此外,您还必须根据QGraphicsTextItem Docs将“ setText”调用更新为“ setPlainText”调用。