-1

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()
Sun B
  • 25
  • 1
  • 7
  • you have to run `insert()` inside `for`-loop. At this moment you runs `insert()` after `for`-loop so it adds only one value - last value – furas Oct 31 '21 at 10:45
  • also I am pretty sure that you don't need to use `.place`, just use `.grid` or `.pack` – Matiiss Oct 31 '21 at 11:58
  • For absolute positioning, wouldn't `.place` be used? – Sun B Oct 31 '21 at 14:21
  • 1
    if you need absolute position then you need `.place` but frankly methods like `grid` and `pack` often are more useful because they can automatically organize widgets when you resize window. And when you use the same code on different systems (Windows, Mac, Linux) then widgets may looks different and absolute position can may not look good. – furas Oct 31 '21 at 15:29

1 Answers1

1

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)
furas
  • 134,197
  • 12
  • 106
  • 148