Python实现VID和PID

问题描述 投票:0回答:1

在我的程序中,每次连接 USB 设备时我都需要打印此内容: 设备名称 | PID|视频

我做了一些研究,但我很困惑。 我有兴趣用 Python 构建这个,不过,如果用其他语言更容易的话, 我想看看如何。

非常感谢!

python usb
1个回答
0
投票
from tkinter import *
from tkinter.ttk import Combobox
from tkinter import ttk
import tkinter as tk
import usb.core
import usb.util
import usb.backend.libusb1

class Window(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.master = master
    
        menu = Menu(self.master)
        self.master.config(menu=menu)
    
        fileMenu = Menu(menu)
        fileMenu.add_command(label="New Window")
        fileMenu.add_command(label="Close window", command=self.exitProgram)
        menu.add_cascade(label="File", menu=fileMenu)

        editMenu = Menu(menu)
        editMenu.add_command(label="Undo")
        editMenu.add_command(label="Redo")
        editMenu.add_separator()
        editMenu.add_command(label="Cut")
        editMenu.add_command(label="Copy")
        editMenu.add_command(label="Paste")
        editMenu.add_command(label="Delete")
        editMenu.add_separator()
        editMenu.add_command(label="Select All")
        editMenu.add_separator()
        editMenu.add_command(label="New Connection")
        editMenu.add_command(label="Send Payload")
        editMenu.add_command(label="Search")
        menu.add_cascade(label="Edit", menu=editMenu)

        viewMenu = Menu(menu)
        viewMenu.add_command(label="Reload")
        viewMenu.add_command(label="Force Reload")
        viewMenu.add_command(label="Toggle Developer Tools")
        viewMenu.add_separator()
        viewMenu.add_command(label="Actual Size")
        viewMenu.add_command(label="Zoom In")
        viewMenu.add_command(label="Zoom Out")
        viewMenu.add_separator()
        viewMenu.add_command(label="Toggle Full Screen", command=self.exitProgram)
        menu.add_cascade(label="View", menu=viewMenu)

        windowMenu = Menu(menu)
        windowMenu.add_command(label="Minimize")
        windowMenu.add_command(label="Zoom")
        windowMenu.add_command(label="Close Window", command=self.exitProgram)
        menu.add_cascade(label="Window", menu=windowMenu)

        helpMenu = Menu(menu)
        helpMenu.add_command(label="Learn More About MQTTX")
        helpMenu.add_command(label="Learn More About EMQX")
        helpMenu.add_command(label="Report Problem")
        helpMenu.add_separator()
        helpMenu.add_command(label="MQTTX Website")
        helpMenu.add_command(label="EMQX Website", command=self.exitProgram)
        menu.add_cascade(label="Help", menu=helpMenu)

        
        options_frame1 = tk.Frame(root, bg='#c3c3c3')
        O_btn = tk.Button(options_frame1, text='O', font=('Bold',15), fg='#158aff', bd=0)
        O_btn.place(x=35, y=50)
        plus_btn = tk.Button(options_frame1, text='+', font=('Bold',15), fg='#158aff', bd=0)
        plus_btn.place(x=35, y=125)
        gl_btn = tk.Button(options_frame1, text='</>', font=('Bold',10), fg='#158aff', bd=0)
        gl_btn.place(x=32.5, y=200)
        l_btn = tk.Button(options_frame1, text='#', font=('Bold',15), fg='#158aff', bd=0)
        l_btn.place(x=35, y=275)
        S_btn = tk.Button(options_frame1, text='*', font=('Bold',15), fg='#158aff', bd=0)
        S_btn.place(x=35, y=375)
        eam_btn = tk.Button(options_frame1, text='<->', font=('Bold',10), fg='#158aff', bd=0)
        eam_btn.place(x=32.5, y=450)
        a_btn = tk.Button(options_frame1, text='@', font=('Bold',10), fg='#158aff', bd=0)
        a_btn.place(x=37, y=525)
        options_frame1.pack(side=tk.LEFT)
        options_frame1.pack_propagate(False)
        options_frame1.configure(width=100, height=600)

        options_frame2 = tk.Frame(root, bg='white')
        L1 = Label(options_frame2, text = 'Device Name',font = 50)
        L1.pack()
        backend = usb.backend.libusb1.get_backend(find_library=lambda x: "./libusb/x64/libusb-1.0.dll")
        devices = usb.core.find(..., backend=backend)
        devices = usb.core.find(find_all=TRUE)
        device = usb.core.find(idVendor=0x1234, idProduct=0x5678)
        cb1 = ttk.Combobox(options_frame2)
        for device in devices: 
           cb1['values']=("VID: {:04x}, PID: {:04x}".format(device.idVendor, device.idProduct))
        '''cb1=Combobox(options_frame2,
                 values=["VID: {:04x}, PID: {:04x}".format(device.idVendor, device.idProduct)],
                 font=('Arial Black', 20),state="readonly")'''
        cb1.pack(padx=5,pady=5)
        B1 = Button(options_frame2, text='connect', font=40)
        B1.pack(padx=7,pady=7 )
        options_frame2.pack(fill=BOTH)
        options_frame2.pack_propagate(True)
        options_frame2.configure(width=1100, height=600)
        

        main_frame = tk.Frame(root)


        main_frame.pack(side=tk.LEFT)
        main_frame.pack_propagate(False)
        main_frame.configure(height=600, width=1100)
        root.geometry('1200x600')
            
           
        
    def exitProgram(self):
        exit()
    
        
root = Tk()
app = Window(root)
root . title("MQTTX")
root . resizable(True, True)
root . mainloop()

    
© www.soinside.com 2019 - 2024. All rights reserved.