Edson Sobreira
Edson Sobreira

Reputation: 1

Tkinter Combobox only shows up the last item

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!

Upvotes: 0

Views: 564

Answers (2)

Edson Sobreira
Edson Sobreira

Reputation: 1

Based on Filip's answer I have tried a second test, creating a list from port and appending in every interation. By mistake I have placed self.TCombobox1['values']=(lst) instead of self.TCombobox1['values']=[lst]. So changing the [lst] for (lst). (paratheses x brakets) I have no idea why became different now, but it worked.

with [lst]-->error

With (lst)--> solved

def check_com(self):
    # Clean list box before send a new command
    self.Listbox1.delete(0,'end')
    lst = []
    for port, desc, hwid in sorted(ports):

        lst.append(port)
        # if I use lst.append[port will not work
        print (lst)
        self.TCombobox1['values']=(lst)
        self.Listbox1.insert("end", port)

Upvotes: 0

Filip Młynarski
Filip Młynarski

Reputation: 3612

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:

def check_com(self):
    # Clean list box before send a new command
    ports = [1,2,3] # created example ports for testing

    self.Listbox1.delete(0,'end')
    self.TCombobox1['values'] = ports

    for port in sorted(ports):
        print(port)
        self.Listbox1.insert("end", port)

Upvotes: 1

Related Questions