How add text without new line in QTextEdit PyQt4 Python 2.7?

妖精的绣舞 提交于 2019-12-11 04:57:00

问题


I have problem with add text without new line in QTextEdit. I must add the value of variable "self.value" and next text ", word" It's my code:

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

class Text(QWidget):
   def __init__(self, parent = None):
      super(Text, self).__init__(parent)

      self.value = 5

      layout = QHBoxLayout()
      self.text_edit = QTextEdit()
      self.text_edit.append(str(self.value))
      self.text_edit.append(",   word")
      layout.addWidget(self.text_edit)
      self.setLayout(layout)
      self.setWindowTitle("TextEdit")

def main():
   app = QApplication(sys.argv)
   ex = Text()
   ex.show()
   sys.exit(app.exec_())

if __name__ == '__main__':
   main()

And I get:

5
,   word

But I would like:

5,   word

Help me, please


回答1:


Easily like this

  self.text_edit = QTextEdit()
  self.text_edit.insertPlainText(str(self.value))
  self.text_edit.insertPlainText(",   word")


来源:https://stackoverflow.com/questions/41547296/how-add-text-without-new-line-in-qtextedit-pyqt4-python-2-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!