Python Forum
[PyQt] populate a QListWidget - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [PyQt] populate a QListWidget (/thread-39761.html)



populate a QListWidget - devilonline - Apr-10-2023

Hi

i want to populate a list widget but it always gives me this error

what can i do?

thanks

error:
Quote:TypeError: addItems(self, Iterable[str]): argument 1 has unexpected type 'str'

code

from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton
from PyQt5 import uic
import sys

from PyQt5.QtWidgets import *



class UI(QMainWindow):
    def __init__(self):
        super(UI, self).__init__()

        uic.loadUi("teste3.ui", self)

        self.button = self.findChild(QPushButton, "pushButton")
        
        self.button.clicked.connect(self.clicker)

        


     

        self.show()

    def clicker (self):
        fruits = ("babana", "orange", "apple")

        self.listWidget = QListWidget()



        for fruit in fruits:
            self.listWidget.addItems(str(fruit))


app = QApplication(sys.argv)
UIWindow = UI()
app.exec_()
thanjs


RE: populate a QListWidget - deanhystad - Apr-10-2023

Post the entire error message. Including the trace. You should not force others to read your code when you can easily provide a concise description of what the error is and where it happened.

The error is very clear. It expects an iterable(str), and you are passing a str. Do you have an iterable thing (like a list perhaps) of strs? You do.
self.listWidget.addItems(fruits)
If you really want to use a list to add items one at a time, you can use self.labelWidget.addItem(str).