views:

106

answers:

1

At program startup, I add some items to my listbox like this:

for widget in WidgetNames:
    listbox.insert(0, widget)

WidgetNames is obviously a list of some items, e.g. "Button" and "Canvas". The thing is, the listbox doesn't show the items that are added with above code. However,

for widget in WidgetNames:
    listbox.insert(0, widget)
    print(listbox.get(0))

prints "Button" and "Canvas", and

for widget in WidgetNames:
    listbox.insert(0, widget)
print(listbox.size())

prints 2, which obviously is the correct number of items it contains. All the listbox shows after items are added is an empty line. I've tried listbox.see(0) and listbox.index(0), but that didn't help. Any ideas why the items aren't added properly?

+1  A: 

The code you're showing is not the problem -- it must be some other code which you are not showing. Please try to reproduce your problem in as small a compass as possible and edit your answer to include that minimal code. Here's a small script to show that the code you show is actually OK:

from Tkinter import *

master = Tk()
listbox = Listbox(master)
listbox.pack()

WidgetNames = ['Button', 'Canvas']
for widget in WidgetNames:
    listbox.insert(0, widget)

mainloop()

This code runs fine on my box (Ubuntu 10.4, Python 2.6) and, exactly as expected, shows the two items ('Canvas' first). If this does not behave that way on your box, please edit your answer to provide the minutest details about said box;-).

Alex Martelli
ARGH!! Okay, it's not tkinter's fault, but mine. I accidentally deleted all items abit later in the program and added an empty one... Sorry :((Well, my cute little program has more than 1800 lines, so I keeping track of everything that happens is pretty tough :/ )
Rawing
@Rawing, well, glad to have helped (by showing it's not Tkinter's fault but yours). To keep your code under control, break it into modules and functions of _far_ smaller sizes than 1800 lines!
Alex Martelli