Background: I am a beginner in Python GUI development with Tkinter. I attempted to populate a Tkinter OptionMenu widget with USB port names retrieved using the serial.tools.list_ports.comports() function. I expected the OptionMenu to display the available USB port names, allowing users to select one. However, I encountered a Tkinter.TclError with the message "bad screen distance" instead.
class App(ctk.CTk):
def __init__(self):
super().__init__()
self.selected_usb_port = ctk.StringVar(self)
self.usb_ports_with_dev = list_uart_ports()
if self.usb_ports_with_dev:
print("USB Ports with '/dev/':", self.usb_ports_with_dev)
self.selected_usb_port.set(self.usb_ports_with_dev[0])
else:
self.selected_usb_port.set("No ports available")
self.usb_ports_option_menu = ctk.CTkOptionMenu(self.topmenu, self.selected_usb_port.get(), *self.usb_ports_with_dev, dynamic_resizing=True, pady=10)
self.usb_ports_option_menu.grid(row=0, column=1, padx=10, pady=10, sticky="nsew")
self.usb_ports_option_menu.place(x=525, y=25)
def list_uart_ports():
uart_ports = []
ports = serial.tools.list_ports.comports()
for port, desc, hwid in sorted(ports):
if "USB" in desc:
if "ttyUSB" in port or "COM" in port:
uart_ports.append(port)
return uart_ports