Using pyudev.pyqt5 within PyQt5's event loop

前端 未结 1 1069
陌清茗
陌清茗 2021-01-20 16:45

I wanted to write a small application that will display usb device\'s name in a small text browser, when it has been inserted. I am using pyudev to do that. Instead of using

相关标签:
1条回答
  • 2021-01-20 17:25

    You need to start the monitor before starting the event-loop. Also, avoid using the @pyqtSlot decorator - it's rarely needed, and it's very easy to get the definition wrong if you don't know what you're doing (as you did in your example).

    So your code should look like this:

    class mainWindow(QWidget, Ui_Form):
        def __init__(self):
            ...
            context = Context()
            monitor = Monitor.from_netlink(context)
            monitor.filter_by(subsystem='tty')
            self.observer = MonitorObserver(monitor)
            self.observer.deviceEvent.connect(self.device_connected)
            monitor.start()
    
        def device_connected(self, device):
            self.textBrowser.append(device.sys_name)
            print("Test")
    
    def main():
        import sys
        app = QApplication(sys.argv)
        window = mainWindow()
        window.show()
        app.exec_()
    

    PS: it might be a good idea to comment out the filter_by line and plug in a usb device just to make sure everything is working okay.

    0 讨论(0)
提交回复
热议问题