我正在尝试在 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_())
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()
关于python - PyQt5:如何在QMessageBox中显示列表?类型错误:参数 3 具有意外类型 'list',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50712783/