質問

I am making a GUI program to detect usb connections using pyudevI. Here is the code :

import gtk
from pyudev import Context,Monitor
from pyudev.glib import GUDevMonitorObserver
import usb.core
import usb.util
import sys

class Project:
    window = gtk.Window()
    vbox= gtk.VBox(False, 5)

    def __init__(self):

        self.window.set_default_size(300, 300)

        label = gtk.Label("Please plug the device")

        self.vbox.pack_start(label)
        self.window.add(self.vbox)

        self.window.show_all()

context = Context()
monitor = Monitor.from_netlink(context)
monitor.filter_by(subsystem='block',device_type='disk')
observer = GUDevMonitorObserver(monitor)

def device_connected(observer, device):
    property_name = device.__getitem__('ID_USB_DRIVER')
    label = gtk.Label('USB Driver::{0!r}'.format(property_name))
    Project.vbox.pack_start(label)
    Project.window.show_all()


observer.connect("device-added",device_connected)
monitor.start()

Project()
gtk.main()

As you will notice, a new label is packed into Project.vbox ONLY ONCE. However in the window, it is displayed twice :

enter image description here

How do I remove the second USB Driver::u'usb-storage'?

役に立ちましたか?

解決

You are packing label twice as your signal obviously gets fired twice. Try to verify how often the signal handler is actually executed.

Check for the devices UUID or serial number and only add the label if the UUID/serial is unknown.

他のヒント

Try: (I haven't been able to test)

def device_connected(observer, device):
    property_name = device.__getitem__('ID_USB_DRIVER')
    label = gtk.Label('USB Driver::{0!r}'.format(property_name))
    Project.vbox.pack_start(label,expand=False)
    Project.window.show_all()
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top