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

问题描述 投票:1回答:2

我有一个清单

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,从中我开始着手解决此帖子的问题:How to print a list with integers without the brackets, commas and no quotes?

python list pyqt pyqt5 qplaintextedit
2个回答
2
投票

您只需要将数字转换为字符串并用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)))

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_())
© www.soinside.com 2019 - 2024. All rights reserved.