Tkinter Entry调用USB检测函数失败,十六进制参数转换出错求助
Alright, let's work through these problems one by one—they're common pitfalls when dealing with string inputs from GUI controls and numerical parameters like VID/PID.
1. The Root Cause of the invalid literal for int() Error
When you enter 0x1447 into the Entry, you're passing a string that represents a hexadecimal value, but Python's int() function defaults to base 10 (decimal) unless you specify otherwise. That's why it throws an error—it can't parse 0x as a decimal digit.
2. Fixing Input Conversion for VID/PID
You need to handle both decimal inputs (e.g., 5191 which is 0x1447 in decimal) and hexadecimal inputs (with or without the 0x prefix). Here's how to adjust your input handling code:
import tkinter as tk from tkinter import messagebox def on_check_USB(): # Get input from Entry widgets and strip whitespace vid_str = vid_entry.get().strip() pid_str = pid_entry.get().strip() # Handle VID: use default if empty, else convert correctly try: if vid_str: if vid_str.startswith('0x'): vid = int(vid_str, 16) # Parse as hex else: vid = int(vid_str, 10) # Parse as decimal else: vid = 0x1447 # Default VID except ValueError: messagebox.showerror("Invalid Input", "VID must be a decimal number or 0x-prefixed hex value") return # Handle PID the same way try: if pid_str: if pid_str.startswith('0x'): pid = int(pid_str, 16) else: pid = int(pid_str, 10) else: pid = 0x8092 # Default PID except ValueError: messagebox.showerror("Invalid Input", "PID must be a decimal number or 0x-prefixed hex value") return # Now call your check_USB function with the correct integer values result = check_USB(vid, pid) # Do something with the result (e.g., show a message) if result: messagebox.showinfo("Success", "USB device detected!") else: messagebox.showwarning("Not Found", "USB device not detected.") # Assume your check_USB function looks like this (example): def check_USB(vid=0x1447, pid=0x8092): # Your existing device detection logic here # For testing: return True if vid/pid match defaults return vid == 0x1447 and pid == 0x8092 # Set up basic Tkinter UI root = tk.Tk() tk.Label(root, text="VID (decimal or 0x-hex):").grid(row=0, column=0) vid_entry = tk.Entry(root) vid_entry.grid(row=0, column=1) tk.Label(root, text="PID (decimal or 0x-hex):").grid(row=1, column=0) pid_entry = tk.Entry(root) pid_entry.grid(row=1, column=1) tk.Button(root, text="Check USB", command=on_check_USB).grid(row=2, columnspan=2) root.mainloop()
3. Why Empty Inputs Weren't Using Defaults
If you were directly passing vid_entry.get() to check_USB, even empty inputs would send an empty string "" instead of letting the function use its default values. The code above explicitly checks if the input is empty and falls back to your default VID/PID integers, which matches how your standalone check_USB function works.
4. Why the Ping Function Worked
Ping functions expect string inputs (like "192.168.1.1"), so passing the raw Entry string directly works. USB VID/PID require integer values though, so the extra conversion step is mandatory.
Quick Debugging Tip
Add a print statement right before calling check_USB to verify your converted values:
print(f"Testing VID: {hex(vid)} ({vid}), PID: {hex(pid)} ({pid})")
This will confirm that you're passing the correct integer values to your detection function.
内容的提问来源于stack exchange,提问作者anxiousPI

