I am trying to implement a Combobox in python app using Tkinter. The main purpose is to show the COM devices connected to the computer (tested with Arduino and micro:bit). Some laptop will show lots of COM port too. I have used also a list box for debug purpose - and looks fine in this part.
Tkinter Combobox and ListBox example
My Code: (sorry it's a little big, because I have made on PAGE.
import serial.tools.list_ports
ports = serial.tools.list_ports.comports()
try:
import Tkinter as tk
except ImportError:
import tkinter as tk
try:
import ttk
py3 = False
except ImportError:
import tkinter.ttk as ttk
py3 = True
class Toplevel1:
def __init__(self, top=None):
top.geometry("600x247+274+330")
top.title("Teste COM")
top.configure(background="#d9d9d9")
self.Btn_COM = tk.Button(top)
self.Btn_COM.place(x=70, y=30, height=24, width=47)
self.Btn_COM.configure(command=self.check_com)
self.Btn_COM.configure(text='''COM''')
self.Btn_COM.configure(width=47)
self.TCombobox1 = ttk.Combobox(top)
self.TCombobox1.place(x=140, y=35, height=21, width=143)
self.Listbox1 = tk.Listbox(top)
self.Listbox1.place(x=415, y=20, height=137, width=119)
self.Listbox1.configure(background="white")
self.Listbox1.configure(width=119)
def check_com(self):
# Clean list box before send a new command
self.Listbox1.delete(0,'end')
for port, desc, hwid in sorted(ports):
print (port)
self.TCombobox1['values']=[port]
self.Listbox1.insert("end", port)
if __name__ == '__main__':
global val, w, root
root = tk.Tk()
top = Toplevel1 (root)
root.mainloop()
Appreciate any help I am using Python 3.7 but I also tested in 2.7 as well.
Thanks!
Your combobox showed only one value because you were overriding its value with every port in your loop, which left it being just one
[port]. Instead you should've set it to list of all of your ports out of your loop like so: