如何将整数列表打印到 QPlainTextEdit?

How to print a list of ints to QPlainTextEdit?

我有一个列表

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

我知道以字符串形式打印到控制台,我会这样做

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

并得到

1
2
3
...

当我将 setPlainText 设置为 QPlainTextEdit 时,我不认为它可以递归完成,我该怎么做?我假设我必须删除逗号和括号并插入 \n,从中我开始寻找解决我的问题的方法 post: How to print a list with integers without the brackets, commas and no quotes?

您只需将数字转换为字符串并添加 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_())

或者正如您指出的那样,您可以使用 join():

w.setPlainText("\n".join(map(str, temp)))
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_())