问题
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