0

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

    cbox = open("krktr.txt")

    for i in cbox.readlines():
        mylist = list()
        mylist.append(i)

        self.comboBox.addItems(str(list))

Please help me with it. Actually, I don't have good English and I'm new here and waiting for someone to help me.

Harsha pps
  • 2,012
  • 2
  • 25
  • 35
  • Use `with open("krktr.txt") as f: mylist = list() for i in f.readlines(): mylist.append(i) self.comboBox.addItems(mylist)` – eyllanesc Jun 20 '19 at 21:45

1 Answers1

1

Try it:

from PyQt5.QtWidgets import QWidget, QApplication, QComboBox, QGridLayout
from PyQt5.QtCore    import Qt

class Widget(QWidget):

    def __init__(self, *args, **kwargs):
        super(Widget, self).__init__(*args, **kwargs)

        with open('krktr.txt') as f:
            myList = [ ''.join(line.split()) for line in f ]

        comboBox = QComboBox()
        comboBox.addItems(myList)
        comboBox.currentTextChanged.connect(self.on_currentTextChanged)

        grid = QGridLayout(self)
        grid.addWidget(comboBox, 0, 0, alignment=Qt.AlignCenter)

    def on_currentTextChanged(self, text):
        print(text)


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

krktr.txt

item1
item2
item3

enter image description here

S. Nick
  • 12,879
  • 8
  • 25
  • 33