1

I'm trying to write the function where the browse button will browse the file and will print its location in the label. However, getting error : AttributeError: 'NoneType' object has no attribute 'pack'. How can this error be resolved and print the location in a label? any help would be appriciated.

    from tkinter import *
    from tkinter import ttk
    from tkinter import filedialog
    
    root = Tk()
    root.geometry('700x650')
    
    def file_opener():
       input = filedialog.askopenfile(initialdir="/")
       print(input)
       for i in input:
          print(i)
    
    label_1 = Label(root, text="Location",width=20,font=("bold", 10))
    label_1.place(x=65,y=130)
    
    x = Button(root, text='Browse',command="file_opener",width=6,bg='gray',fg='white').place(x=575,y=130)
    x.pack()
    entry_1 = Entry(root)
    entry_1.place(x=240,y=130,height=20, width=300)
    
    root.mainloop()

1 Answers1

0

When you wrote this:

x = Button(...).place(...)

it actually means:

temp = Button(...)
x = temp.place(...) # Always `None`

That is why it is telling you that the variable x is None. To fix it change that line to:

x = Button(...)
x.place(...)

Also it's never a good idea to mix geometry managers like pack, grid, and place. Edit

There a few other problems with your code:

  1. You need to have command=file_opener instead of command="file_opener"
  2. Change filedialog.askopenfile to filedialog.askopenfiles if you want the user to be able to select multiple files
TheLizzard
  • 7,248
  • 2
  • 11
  • 31
  • Hey, thanks I'm able to launch the window, but the problem is the "browse" button not working, and not able to explore. Anything wrong with the above code? and how to update that file location to label? –  Mar 08 '21 at 09:35
  • @Shantanu I edited my answer. Basically now your problem is that you have `command="file_opener"` instead of `command=file_opener` – TheLizzard Mar 08 '21 at 09:37