简体   繁体   中英

Only last value displays in tkinter listbox

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()

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM