Home > Software design >  Only last value displays in tkinter listbox
Only last value displays in tkinter listbox

Time:11-01

I iterate through either key or values from my config.ini file and insert it into my listbox, however, only the last value is inserted.

My config files has two sections [TEST1] and [TEST2]. Only TEST2 shows in the listbox, printing the keys however, prints both TEST1 and TEST2.

Thank you in advance.

from tkinter import *
from configobj import ConfigObj

root = Tk()

parser = ConfigObj("config1.ini")
options = parser.iterkeys()

for k in options:
    print(k)

listbox = Listbox(root, width=50)
listbox.place(x=145, y=230)
listbox.insert('end', k)

root.title('Test')
root.geometry('500x500')
root.mainloop()

CodePudding user response:

You have to run insert() inside for-loop.

listbox = Listbox(root, width=50)
listbox.place(x=145, y=230)

for k in options:
    listbox.insert('end', k)
    print(k)
  • Related