Choose databits from combobox

佐手、 提交于 2019-12-11 07:33:13

问题


I have a simple GUI for serial communication with an Arduino UNO. First I list all databit in an OrderedDict, then I put them in a combobox:

self.databits = OrderedDict([
            ('5', QtSerialPort.QSerialPort.Data5),
            ('6', QtSerialPort.QSerialPort.Data6),
            ('7', QtSerialPort.QSerialPort.Data7),
            ('8', QtSerialPort.QSerialPort.Data8)
        ])
        self.databit_comboBox.addItems(list(self.databits))

Then I run the code, I select an option from databits combobox (this is how I get data from combobox)

self.serial.setDataBits(self.databit_comboBox.currentData())

and when I press connect button, I get this error:

TypeError: setDataBits(self, QSerialPort.DataBits): argument 1 has unexpected type 'NoneType'

回答1:


When you add data with addItems() method you only add text, so you will discard the second part. The solution is to iterate and separate each part:

databits = [
        ('5', QtSerialPort.QSerialPort.Data5),
        ('6', QtSerialPort.QSerialPort.Data6),
        ('7', QtSerialPort.QSerialPort.Data7),
        ('8', QtSerialPort.QSerialPort.Data8)
]
for text, databit in databits:
    self.databit_comboBox.addItem(text, databit)

# ...

self.serial.setDataBits(self.databit_comboBox.currentData())


来源:https://stackoverflow.com/questions/55552298/choose-databits-from-combobox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!