1

I have a list

temp = [1, 2, 3, 4, 5, 6, 7, 8] 

I know to print to the console as strings, I would just do

for i in range(0, len(temp)):
    temp[i] = str(temp[i])

and get

1
2
3
...

How can I do that since I don't think it can be done recursively when I setPlainText to the QPlainTextEdit? I assume I would have to remove the commas and brackets and insert \n, from which I started looking to solve my problems with this post: How to print a list with integers without the brackets, commas and no quotes?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
geistmate
  • 525
  • 2
  • 5
  • 14

2 Answers2

2

You just have to convert the numbers to string and add it with appendPlainText():

import sys
from PyQt5 import QtWidgets


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)

    temp = [1, 2, 3, 4, 5, 6, 7, 8]

    w = QtWidgets.QPlainTextEdit()
    for i in temp:
        w.appendPlainText(str(i))
    w.show()
    sys.exit(app.exec_())

Or as you point out you can use join():

w.setPlainText("\n".join(map(str, temp)))
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
0
import sys
from PyQt5.QtWidgets import (QPlainTextEdit, QApplication)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    temp = [1, 2, 3, 4, 5, 6, 7, 8]
    w = QPlainTextEdit()
    while temp:
        w.appendPlainText(str(temp.pop(0)))
    w.show()
    sys.exit(app.exec_())
oetzi
  • 1,002
  • 10
  • 21