Goulouh Anwar
Goulouh Anwar

Reputation: 767

populate a list box with .csv items

I have this.

scrollbar = tk.Scrollbar(listbox)
scrollbar.pack(side=RIGHT, fill=Y)

listbox = tk.Listbox(root)
listbox.insert(1,'a')
listbox.pack()

listbox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=listbox.yview)

with open('testCUR.csv', newline='') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
    for row in spamreader:
        print(', '.join(row),'\n')

I was wondering how to correctly populate the listbox with values from a .csv file?

And also why I cannot use the scroll bar correctly?

Once the list box is populated I would like to put the value of the last cell from the selection to a variable for use in a URL string. I havent found any tutorials for this so was looking for help here.

I have tried this.

listbox = tk.Listbox(root, height=1)

listbox.place(x=300,y=75)


with open('testCUR.csv', 'r') as f:
  reader = csv.reader(f)
  your_list = list(reader)
  for item in your_list:

    listbox.insert(end, item)

or this inputs only first entry

with open('testCUR.csv', 'r') as f:
  reader = csv.reader(f)
  your_list = list(reader)
  for item in your_list:

    listbox.insert(1, item)

Once I have all the values in the list box and it is scrollable which I would love if it was just damn.

listbox = tk.Listbox(root, height=1, scroll=auto)

I need to be able to use only the currency code which is the last value in the .csv file and use it as the selected value and then use it in a variable for the url. The .csv file looks like this.

Algeria د.ج DZD
Andorra €   EUR
Angola  Kz  AOA
Anguilla    $   XCD
Antigua and Barbuda $   XCD
Argentina   $   ARS
Armenia     AMD
Aruba   ƒ   AWG
Ascension Island    £   (*none*)

I am also trying pandas but am new to it because it looks much easier and cleaner to use.

csv_file = ('testCUR.csv')

df = pd.read_csv(csv_file)

saved_col = df['CODE']

for item in df:
    listbox.insert(end, saved_col)

Always error. NameError: name 'end' is not defined. Happens with END also

Upvotes: 0

Views: 2281

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386342

I was wondering how to correctly populate the listbox with values from a .csv file?

To insert text into a listbox you must give it an index to tell it where to insert. The index is a string that is either a number, or the string "end". In your case you used a variable named end, which of course doesn't exist.

You can insert the text like this:

listbox.insert("end", item)

And also why I cannot use the scroll bar correctly?

You haven't described why your scrollbar is not correct.

Making a scrollbar works requires two-way conversation. The scrollbar must be told what widget to scroll (via the command attribute, and the widget needs to know which scrollbar to update when it is scrolled (via the yscrollcommand or xscrollcommand attribute).

It's also good to explicitly set whether the scrollbar is horizontal or vertical, though in your case it's vertical which is the default.

And finally, it's generally the best practice to make the scrollbar and the widget being scrolled to have the same parent. You made the mistake of making the scrollbar a child of the listbox. Instead, make it a child of whatever the listbox is a child of. You also made the mistake of trying to make it the parent of the listbox before you created the listbox. A widget must exist before you can give it children.

Here is how to create the listbox and scrollbar:

listbox = tk.Listbox(root)
scrollbar = tk.Scrollbar(root, orient="vertical", command=listbox.yview)
listbox.configure(yscrollcommand=scrollbar.set)

Upvotes: 2

Related Questions