【问题标题】:AttributeError: 'MainWindow' object has no attribute 'dev'AttributeError:“MainWindow”对象没有属性“dev”
【发布时间】:2014-08-14 18:25:24
【问题描述】:

嘿, 是的,我正在编写一个 python3 程序,它应该将文件的内容发送到 LPC1758。 为了测试,我将在 USB 上方写一个简单的字符串。我用 libusb1.0 ....

但是每次我变成同样的错误,即使我将“dev”重命名为“device”,错误也是一样的。 --> AttributeError: 'MainWindow' 对象没有属性 'dev'

class MainWindow(QtGui.QMainWindow, form_class):
def __init__(self, parent=None):
    QtGui.QMainWindow.__init__(self, parent)
    self.setupUi(self)
    self.pb_send.clicked.connect(self.pb_send_clicked)
    self.pb_open.clicked.connect(self.pb_open_clicked)
    self.pb_exit.clicked.connect(self.pb_exit_clicked)


  # SEND button event handler
def pb_send_clicked(self):

    send = "Yo Man"

    bulk_out_ep = 0x05
    bulk_in_ep = 0x82
    interface = 0
    length = 30

    dev = usb.core.find(idVendor= 0xfefe, idProduct= 0x0001)
    cfg = usb.control.get_configuration(dev)            # get current configuration

    print(dev)


    if dev is None:
        self.USB_label.setText("Device not found !!!")
        raise ValueError('Device not found')
    else:
        self.USB_label.setText("Device found")
        found = 1

    if(cfg != 1):                                       # check if device is configured
        usb.DeviceHandle.setConfiguration(self, 1)

    usb.util.claim_interface(dev, interface)

    usb.DeviceHandle.bulkWrite(self,bulk_out_ep,send)
    print("wrote to estick")
    readData = usb.DeviceHandle.bulkRead(self, bulk_in_ep, length)
    print("read from estick: "+ readData)

    usb.util.release_interface(dev, interface)

这是 Stacktrace 显示的内容:

Traceback (most recent call last):
  File "/home/user/workspace_Qt/ESS_project/ess_project.py", line 93, in pb_send_clicked
    readData = usb.DeviceHandle.bulkRead(self,endpoint=bulk_in_ep,size=length)
  File "/usr/lib/python3.3/usb/legacy.py", line 159, in bulkRead
    return self.dev.read(endpoint, size, self.__claimed_interface, timeout)
AttributeError: 'MainWindow' object has no attribute 'dev'

【问题讨论】:

  • 我不认为错误来自提供的代码,因为我在这段代码中看不到任何可能导致上述错误的内容。请通过查看stacktrace 并找到错误的行号,确保您从这段代码中得到了该错误。
  • 这是我的堆栈跟踪输出的内容:
    回溯(最后一次调用):
    文件“/home/hummer/workspace_Qt/ESS_project/ess_project.py”,第 93 行,在 pb_send_clicked
    readData = usb.DeviceHandle.bulkRead(self,endpoint=bulk_in_ep,size=length)
    文件“/usr/lib/python3.3/usb/legacy.py”,第 159 行,在bulkRead
    return self.dev.read(endpoint, size, self.__claimed_interface, timeout)
    AttributeError: 'MainWindow' object has no attribute 'dev'
    我不知道这是什么意思。 ..

标签: python-3.x pyqt4 pycharm xubuntu


【解决方案1】:

devDeviceHandle 类的成员,这是 setConfiguration 函数所期望的第一个参数的类型。

setConfiguration 需要 2 个参数而不是 1 个参数,因为您是在 usb.DeviceHandle 类上调用方法而不是在 usb.DeviceHandle 类型的对象上调用它,您应该使用:

dev.setConfiguration(1)

bulkWritebulkRead 也一样)。

同样get_configuration 方法返回一个Configuration 对象而不是一个数字,所以cfg != 1 将始终为真(这可能与cfg.index != 1 一起使用,但我不确定)。

【讨论】:

  • 我试过但没有成功。当我从 bulkRead 和 buldWrite 中删除“self”属性时,我收到一个错误,即缺少属性...其他想法?
  • @InvAdErZz 我编辑了答案。我错误地认为usb.DeviceHandle 是您的对象之一,而不是一个类。
  • 哦,对不起.. 好事:现在我更聪明了坏事:问题仍然存在 :( 设备对象可以具有哪些属性??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-01
  • 2021-04-19
  • 2021-11-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多