从组合框中选择数据位 [英] Choose databits from combobox

查看:66
本文介绍了从组合框中选择数据位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 GUI,用于与 Arduino UNO 进行串行通信.首先,我在 OrderedDict 中列出所有数据位,然后将它们放入组合框中:

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'

推荐答案

当您使用 addItems() 方法添加数据时,您只会添加文本,因此您将丢弃第二部分.解决方案是迭代和分离每个部分:

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())

这篇关于从组合框中选择数据位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆