python tkinter listbox: adding items

17,631

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;-).

Share:
17,631
Rawing
Author by

Rawing

Updated on June 04, 2022

Comments

  • Rawing
    Rawing almost 2 years

    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?