在 PyQt5 的事件循环中使用 pyudev.pyqt5 [英] Using pyudev.pyqt5 within PyQt5's event loop

查看:87
本文介绍了在 PyQt5 的事件循环中使用 pyudev.pyqt5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个小型应用程序,当插入时,它会在小型文本浏览器中显示 USB 设备的名称.我正在使用 pyudev 来做到这一点.我发现我们可以使用 MonitorObserver 将 pyudev 集成到 qt 的事件循环中,而不是使用 pyudev 自己的事件循环.我一直在尝试这样做,但结果是徒劳的.这是我迄今为止尝试过的代码.如果我做错了,请告诉我.

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 pyudev's own event loop, i found out that we can integrate pyudev into qt's event loop, by using MonitorObserver. I have been trying to do that, but the results are unfruitful. Here is the code that i have tried so far. Please tell me if i am making a mistake.

进口

from PyQt5.QtCore import pyqtSignal,pyqtSlot,QObject,QSocketNotifier
from PyQt5.QtWidgets import QWidget,QApplication
from form_designer import Ui_Form
from pyudev.pyqt5 import MonitorObserver
from pyudev import Context,Monitor
from PyQt5 import QtCore

小班

class mainWindow(QWidget,Ui_Form):
    def __init__(self,monitor):
        super().__init__()

        self.setupUi(self)
        print("First test")

        monitor.filter_by(subsystem='tty')
        self.observer = MonitorObserver(monitor)
        print("second test")

        self.observer.deviceEvent.connect(self.device_connected)
    @pyqtSlot()
    def device_connected(self,device):
        self.textBrowser.append(device.sys_name)
        print("Test")

我什至使用打印进行了测试,以查看循环进行了多远.这里是初始化和主要

I even tested using prints to see how far the loop was going. Here is the initialization and main

def main():
    import sys
    app = QApplication(sys.argv)
    context = Context()
    monitor = Monitor.from_netlink(context)
    window = mainWindow(monitor)
    window.show()

    app.exec_()
    monitor.start()
if __name__ == '__main__':
    main()

执行时,它会打印前两个打印件,并且永远不会调用插槽.

When executed, it prints the first two prints and the slot never gets called.

仅供参考:我开始学习 Python 已经一周了.

FYI : Its been a week since i started learning python.

推荐答案

您需要在启动事件循环之前启动监视器.此外,避免使用 @pyqtSlot 装饰器 - 很少需要它,如果您不知道自己在做什么(就像您在示例中所做的那样),很容易弄错定义.

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:将 filter_by 行注释掉并插入 USB 设备以确保一切正常,这可能是个好主意.

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.

这篇关于在 PyQt5 的事件循环中使用 pyudev.pyqt5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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