如何解决这些组合框str类型问题?

时间:2019-06-20 18:09:44

标签: pyqt pyqt5

TypeError:addItems(self,Iterable [str]):参数1具有意外的类型'str'

    cbox = open("krktr.txt")

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

        self.comboBox.addItems(str(list))

请帮助我。实际上,我的英语不好,而且我是新来的,正在等别人帮助我。

1 个答案:

答案 0 :(得分:0)

尝试一下:

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