微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 PyQt 中使用连接到触发槽的自定义函数在运行时动态创建 QAction

如何解决如何在 PyQt 中使用连接到触发槽的自定义函数在运行时动态创建 QAction

在我的程序中,我在 comList 中收到 Windows 上可用的通讯列表。我需要使用连接到其触发插槽的自定义函数制作自定义 QAction。我遇到的问题是我只能通过引用传递变量,所以当涉及到执行时间时,每个 QAction 的 comList[-1] 都是相同的。有什么办法可以返回 comList[-1] 的值而不是它在 python 中的引用?

def findComs():
    #get list of com ports
    comList = cf.serial_ports()
    print(comList)
    #delete existing actions
    connectMenu.clear()
    #create action for each port on the list and add to connectMenu
    while len(comList) > 0:
        action = qw.QAction(comList[-1],self)
        action.triggered.connect(lambda: self.__optionUpdated("com",'"{0}"'.format(comList[-1])))
        connectMenu.addAction(action)
        print(comList)
        comList.pop()
                

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。