python tkinter listbox - insert error -


this question has answer here:

i cannot 'insert' string tkinter listbox. code is:

from tkinter import * tkfiledialog import askopenfilenames  def importfiles():     tk().withdraw()     filenames = askopenfilenames()     f in filenames:         listbox1.insert(end, f)     return  root = tk() root.geometry("500x800")  listbox1 = listbox(root).grid(row=1, sticky=w, padx=20, pady=20) button1 = button(root, text='import', command=importfiles).grid(row=2, sticky=w, padx=20)  root.mainloop() 

when run snippet of code gui pops fine. press button , and open file dialog comes up, expected. when select files , press 'ok' code bombs. following error:

file "c:\python27\lib\lib-tk\tkinter.py", line 1536, in call

return self.func(*args)

file ".../pygui_01.py", line 10, in importfiles

listbox1.insert(end, f)

attributeerror: 'nonetype' object has no attribute 'insert'

i don't understand. set object 'listbox1' tk listbox. there no indication in ide command 'insert' not valid (pycharm). doing wrong?

you setting variable "listbox1" result of grid(row=1, sticky=w, padx=20, pady=20).

try 2 lines:

listbox1 = listbox(root) listbox1.grid(row=1, sticky=w, padx=20, pady=20) 

now listbox1 set instance of listbox, , not result of "grid()."


Comments