【问题标题】:PyQt5: How to display list in QMessageBox? TypeError: argument 3 has unexpected type 'list'PyQt5:如何在 QMessageBox 中显示列表?类型错误:参数 3 具有意外类型“列表”
【发布时间】:2018-06-06 05:26:55
【问题描述】:

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

TypeError:问题(QWidget,str,str,按钮: Union[QMessageBox.StandardButtons, QMessageBox.StandardButton] = QMessageBox.StandardButtons(QMessageBox.Yes|QMessageBox.No), 默认按钮:QMessageBox.StandardButton = QMessageBox.NoButton): 参数 3 具有意外类型“列表”

代码如下:

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_())

框上的输入是这些行:

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上显示以包含列表,谢谢你的帮助

【问题讨论】:

    标签: python python-3.x pyqt pyqt5 qmessagebox


    【解决方案1】:

    一种可能的解决方案是使用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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-22
      • 2012-09-14
      • 2018-12-01
      相关资源
      最近更新 更多