How to add columns to a tkinter.Listbox?
You could turn the csv file into a dictionary, use the combined country and currency codes as the keys and just the codes as the values, and finally insert the keys into the Listbox
. To get the code of the current selection, you can do this: currencies[listbox.selection_get()]
.
listbox.selection_get()
returns the key which you then use to get the currency code in the currencies
dict.
import csvimport tkinter as tkroot = tk.Tk()currencies = {}with open('testCUR.csv') as f: next(f, None) # Skip the header. reader = csv.reader(f, delimiter=',') for country, code in reader: currencies[f'{country} {code}'] = codelistbox = tk.Listbox(root)for key in currencies: listbox.insert('end', key)listbox.grid(row=0, column=0)listbox.bind('<Key-Return>', lambda event: print(currencies[listbox.selection_get()]))tk.mainloop()