python - PyQt5:如何在QMessageBox中显示列表?类型错误:参数 3 具有意外类型 'list'

标签 python python-3.x pyqt pyqt5 qmessagebox

我正在尝试在 QMessageBox 中显示列表 我不断收到错误:

TypeError: question(QWidget, str, str, buttons: Union[QMessageBox.StandardButtons, QMessageBox.StandardButton] = QMessageBox.StandardButtons(QMessageBox.Yes|QMessageBox.No), defaultButton: QMessageBox.StandardButton = QMessageBox.NoButton): argument 3 has unexpected type 'list'

代码如下:

import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QPushButton, QAction, QLineEdit, QMessageBox
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot

class App(QMainWindow):

    def __init__(self):
        super().__init__()
        self.title = 'List manipulation'
        self.left = 10
        self.top = 10
        self.width = 325
        self.height = 300
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        # Create textbox
        self.textbox = QLineEdit(self)
        self.textbox.move(20, 20)
        self.textbox.resize(280, 200)

        # Create a button in the window
        self.button = QPushButton('Show text', self)
        self.button.move(20, 230)

        # connect button to function on_click
        self.button.clicked.connect(self.on_click)
        self.show()

    @pyqtSlot()
    def on_click(self):
        textboxValue = self.textbox.text()
        xlist = textboxValue.splitlines()
        xlist_final=[]
        for xitem in xlist:
            if xitem.find("abc") != -1:
                xlist_final.append(xitem)
    QMessageBox.question(self, 'List manipulation', xlist_final, QMessageBox.Ok, QMessageBox.Ok)
    self.textbox.setText("")


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

enter image description here 框中的输入如下:

abc
xabcx
def
zabc
ghi
ooabc
abccc

我想过滤上面的列表以仅显示包含“abc”的单词 结果将是:

abc
xabcx
zabc
ooabc
abccc

我已经在上面的python文件中编写了代码,如下

textboxValue = self.textbox.text()
            xlist = textboxValue.splitlines()
            xlist_final=[]
            for xitem in xlist:
                if xitem.find("abc") != -1:
                    xlist_final.append(xitem)

问题是如何在Qmessagebox上显示包含列表,谢谢您的帮助

最佳答案

一个可能的解决方案是使用 join() 将字符串与 \n 连接起来,如下所示:

@pyqtSlot()
def on_click(self):
    textboxValue = self.textbox.text()
    xlist = textboxValue.splitlines()
    xlist_final = [xitem for xitem in xlist if xitem.find("abc") != -1]
    QMessageBox.question(self, 'List manipulation', "\n".join(xlist_final), QMessageBox.Ok, QMessageBox.Ok)
    self.textbox.clear()

enter image description here

关于python - PyQt5:如何在QMessageBox中显示列表?类型错误:参数 3 具有意外类型 'list',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50712783/

相关文章:

python - 另一个函数定义中的函数定义 - 慢?

python - Flask abort() 或返回状态之间的区别

python - 在 python 上循环

python - QApplication.setOverrideCursor对该函数没有任何作用,为什么?

qt - 从模型中删除的行仍在 View 中,我做错了什么?

python - 使用 PyQt5 轻松进行多线程,用于更新 QTextBrowser 内容

python - 将 Python namedtuple 序列化为 json

python - 如果可能,如何将 Python dict 转换为 JSON 作为列表

python - 有没有办法可以使用用户输入批量重命名文件夹中的文件?

python - 在python-sell中运行python脚本时,Python OpenCv读取错误