-1

I would like my Listbox widget to be updated upon clicking of a button. However I encountered a logic error. When I click on the button, nothing happens. No errors at all.

listOfCompanies: [[1, ''], [2, '-'], [3, '@ASK TRAINING PTE. LTD.'], [4, 'AAIS'], [5, 'Ademco'], [6, 'Anacle']

def populatebox():
            listBox.insert("end", listOfCompanies)

btn = Button(self, text="Update list", command = lambda: populatebox())
btn.pack()
3
  • I don't believe that you don't get an error. listOfCompanies is a list which does not exist inside the function populatebox() so when calling it inside that function it returns an error NameError: name 'listOfCompanies' is not defined. So you are guaranteed to return an error with the code you provided us. Commented Oct 17, 2017 at 10:09
  • read-access to the list is possible even if the list is not defined within populatebox() - so Python is not going to complain about that Commented Oct 17, 2017 at 10:15
  • @DonGru The list doesn't exist, full stop. She has declared it incorrectly. Commented Oct 17, 2017 at 10:17

1 Answer 1

5

If you're looking to just insert every tuple into the Listbox from the list as they are without separating out the tuple then there are two major changes.

First you cannot declare a list as list: [1, 2, 3, ...], it must be list = [1, 2, 3, ...].

Secondly, you are currently attempting to insert the entire list onto one entry in the Listbox. You should instead iterate over them, see below for an example:

from tkinter import *

root = Tk()

listBox = Listbox(root)
listBox.pack()

listOfCompanies = [[1, ''], [2, '-'], [3, '@ASK TRAINING PTE. LTD.'], [4, 'AAIS'], [5, 'Ademco'], [6, 'Anacle']]

def populatebox():
    for i in listOfCompanies:
        listBox.insert("end", i)

btn = Button(root, text="Update list", command = lambda: populatebox())
btn.pack()
Sign up to request clarification or add additional context in comments.

2 Comments

Actually, I got the list from reading a .csv file however I did not include it in the original post as I thought it would make the post too lengthy and it was irrelevant. Either way, I tried placing the code you've suggested and similarly, it gave no output and no error when I clicked on the button.
If this solution answers your question, please can you mark it as accepted for future users.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.