我已经有一些可用的 Python 代码来检测某些 USB 设备类型的插入(来自此处)。
import wmi
raw_wql = "SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE TargetInstance ISA \'Win32_USBHub\'"
c = wmi.WMI()
watcher = c.watch_for(raw_wql=raw_wql)
while 1:
    usb = watcher()
    print(usb)
Run Code Online (Sandbox Code Playgroud)
不幸的是,这个脚本没有检测到所有类型的 USB 设备的插入。这意味着检测到 USB 闪存驱动器的插入,但未检测到 USB 输入设备。根本没有检测到 USB 设备的移除。
有没有办法相应地扩展现有脚本?
编辑:更好的 WQL 查询和 Python 代码
我根据在MSDN 中获得的信息改进了 WQL 查询和 Python 代码。
以下脚本旨在在插入或拔出 USB 键盘时输出消息。
问题:插入 USB 键盘时不显示消息,但拔下 USB 键盘时会出现两条消息(“键盘已连接”和“键盘已断开”)。这段代码有什么问题?
import wmi
device_connected_wql = "SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE TargetInstance ISA \'Win32_Keyboard\'"
device_disconnected_wql …Run Code Online (Sandbox Code Playgroud) 我想禁用 QGraphicsView 中的鼠标指针。
在下面的例子中我需要添加哪行代码?
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QGraphicsView
class GraphicsWindow(QGraphicsView):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.showFullScreen()
    def keyPressEvent(self, event):
        if event.key() == Qt.Key_Escape:
            self.close()
if __name__ == "__main__":
    app = QApplication(sys.argv)
    graphics_window = GraphicsWindow()
    graphics_window.show()
    sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud) 我无法获得以下用于检测 USB 设备插入的代码,以便在使用 Python 3.7 的 Windows 10(64 位)计算机上运行。
import win32serviceutil
import win32service
import win32event
import servicemanager
import win32gui
import win32gui_struct
struct = win32gui_struct.struct
pywintypes = win32gui_struct.pywintypes
import win32con
GUID_DEVINTERFACE_USB_DEVICE = "{A5DCBF10-6530-11D2-901F-00C04FB951ED}"
DBT_DEVICEARRIVAL = 0x8000
DBT_DEVICEREMOVECOMPLETE = 0x8004
import ctypes
#
# Cut-down clone of UnpackDEV_BROADCAST from win32gui_struct, to be
# used for monkey-patching said module with correct handling
# of the "name" param of DBT_DEVTYPE_DEVICEINTERFACE
#
def _UnpackDEV_BROADCAST (lparam):
  if lparam == 0: return None
  hdr_format = …Run Code Online (Sandbox Code Playgroud) 下面给定的代码显示一个QMainWindow4QGraphicsView以在其中使用鼠标进行绘制。它按预期工作,但关闭它时,控制台中会出现以下错误消息:
Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
Run Code Online (Sandbox Code Playgroud)
代码有什么问题?
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QGraphicsView, QGraphicsScene, QGraphicsPathItem
from PyQt5.QtGui import QPainterPath, QPen
from PyQt5.QtCore import Qt
from PyQt5.uic import loadUi
# Based on code from /sf/answers/3097415611/
class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        loadUi("mainwindow.ui", self)
        self.verticalLayout_top_left.addWidget(GraphicsView())
        self.verticalLayout_top_right.addWidget(GraphicsView())
        self.verticalLayout_bottom_left.addWidget(GraphicsView())
        self.verticalLayout_bottom_right.addWidget(GraphicsView())
class GraphicsView(QGraphicsView):
    def __init__(self):
        super().__init__()
        self.start = None
        self.end = None
        self.setScene(QGraphicsScene())
        self.path = QPainterPath()
        self.item = GraphicsPathItem()
        self.scene().addItem(self.item)
        self.contents_rect = self.contentsRect() …Run Code Online (Sandbox Code Playgroud) 我想使用Tkinter向我的Python GUI添加一个循环进度条,但是我没有找到任何有关Tkinter的循环进度条的文档.
如何在Tkinter中创建循环进度条或这是不可能的?
python ×4
pyqt5 ×2
usb ×2
windows-10 ×2
crash ×1
progress-bar ×1
pyqt ×1
python-3.x ×1
qt5 ×1
tkinter ×1