1

For some reason in the listbox there seems to be brackets around a string that ends with a space.

from Tkinter import *

def update_listbox():   
    for i in dic:
        listbox.insert(END, (i, dic[i][0], dic[i][1], dic[i][2]))

dic = {}
dic['Foods'] = ['apple','grape','pizza']
dic['Drinks      '] = ['milk','soda','jucie'] # How do i make the {} not show up but keep the spaces?

root=Tk()
listbox = Listbox(root)
button = Button(root,text='push',command=update_listbox)
button.pack()
listbox.pack()


root.mainloop()

1 Answer 1

5

I think it's got something to do with you inserting a tuple element into the listbox rather than one or more strings. Try this instead:

listbox.insert(END, ' '.join([i]+dic[i])

which concatenates the key and all the sub-elements together into a single string -- which also keeps them all on one line -- and inserts that single thing into the listbox. Here's what it looks like on my system after pushing the button:

listbox with no brackets inserted

The other alternative would be to unpack the tuple in the call:

listbox.insert(END, *(i, dic[i][0], dic[i][1], dic[i][2]))

or, better yet, not put them in a tuple in the first place:

listbox.insert(END, i, dic[i][0], dic[i][1], dic[i][2])

But even better than that, because it would be able to handle lists of any length (as pointed out by @mgilson in the comments) would be to write it this way:

listbox.insert(END, i, *dic[i])

Regardless, the result would look like this:

another listbox with no brackets inserted each word on a separate line

Here's a way to do things so the listbox will automatically update its width after it's updated. It also shows another way to build the dictionary and later iterate through its contents when populating the listbox:

from Tkinter import *

def update_listbox():
    longest = 20  # initial value acts as minimum width
    for key,value in dic.iteritems():
        entry = '{}: {}'.format(key, (', '.join(value)))
        longest = max(longest, len(entry))
        listbox.insert(END, entry)
    listbox.config(width=longest)  # width in "standard" or average characters

dic = {
    'Foods': ['apple','grape','pizza'],
    'Drinks': ['milk','soda','juice'],
}

root=Tk()
root.title('Listbox')
listbox = Listbox(root)
button = Button(root, text='push', command=update_listbox)
button.pack()
listbox.pack()

root.mainloop()

With this result:

listbox final version

BTW, here's a good online Tkinter reference.

Sign up to request clarification or add additional context in comments.

8 Comments

or listbox.insert(END, i, *dic[i]) -- this way you can have as many Foods or Drinks as you want!
@mgilson: So does listbox.insert(END, ' '.join([i]+dic[i]) in my edit from now 6 hours ago.
That stacks them horizontally, not vertically. It's a little unclear what the intention was...but if stacking vertically was intented, my suggestion will work with any number of items.
@mgilson: The version in my edit works for any number of items, too. dic[i] can be a list of any size.
you mean: listbox.insert(END, ' '.join([i]+dic[i])? That puts all the d[i] on the same line since it joins them together before inserting. My proposed solution adds each item in d[i] as a new row in the listbox which is different than what you're doing.
|

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.