-2

Hi I am new to the tkinter and trying out a simple Tkinter GUI, but getting error:

TypeError: 'NoneType' object is not callable

This is my code:

from tkinter import *
from tkinter import filedialog
import pandas as pd
import pyodbc
from sqlalchemy import create_engine
import urllib

master = Tk()
master.title("Demo GUI")
master.geometry("900x400+150+150")
master.resizable(0,0)

def browse_file():
    global file_path
    global data_frame
    file_path = filedialog.askopenfilename(title = "Choose the file to upload")
    data_frame = pd.read_excel(file_path)

Label = Label(master,text="Choose the file to upload").grid(row=0)

Button = Button(master,text='Browse',command = browse_file).grid(row=0,column=1,pady=4)

Label_1 = Label(master,text="The file selected: "+file_path).grid(row=1,column=0)

master.mainloop()

The error that I am getting is,

TypeError                                 Traceback (most recent call last)
<ipython-input-29-451372edd65a> in <module>
     25 Button = Button(master,text='Browse',command = browse_file).grid(row=0,column=1,pady=4)
     26 
---> 27 Label1 = Label(master,text="The file Choosen: "+file_path).grid(row=1,column=0)
     28 
     29 

TypeError: 'NoneType' object is not callable
James Z
  • 12,209
  • 10
  • 24
  • 44
  • Does this answer your question? [AttributeError: NoneType object has no attribute](https://stackoverflow.com/a/1101765/7414759) – stovfl May 01 '20 at 10:17
  • You are setting `Label` to `None`, and then trying to create a label using `Label(...)`. – Bryan Oakley May 01 '20 at 15:50

3 Answers3

0

The error you did is a typo: By writing:

Label = Label(master,text="Choose the file to upload").grid(row=0)

You assign the result of the grid call to the original tk.Label type (Label). gird return call is None

So then when you try to create the Label1 you are in practice calling Label which is now None

Simply replace the line by:

 l = Label(master,text="Choose the file to upload")
 l.grid(row=0)

or simply

Label(master,text="Choose the file to upload").grid(row=0)
Jean-Marc Volle
  • 3,113
  • 1
  • 16
  • 20
0

'NoneType' object is not callable error is caused by placing object where it is defined. So instead of

Label_1 = Label(master,text="The file selected: "+file_path).grid(row=1,column=0)

Try:

Label(master,text="The file selected: "+file_path).grid(row=1,column=0)

Or

Label_1 = Label(master,text="The file selected: "+file_path)
Label_1.grid(row=1,column=0)

Also do not use Button = Button(master... rather give a unique name to variable

0

This is answer:

from tkinter import *
from tkinter import filedialog
import tkinter as tk
import pandas as pd
import pyodbc
from sqlalchemy import create_engine
import urllib

master = Tk()
master.title("Demo GUI")
master.geometry("900x400+150+150")
master.resizable(0,0)

def browse_file():
    global file_path
    global data_frame
    file_path = filedialog.askopenfilename(title = "Choose the file to upload")
    data_frame = pd.read_excel(file_path)

Label = Label(master,text="Choose the file to upload").grid(row=0)

Button = Button(master,text='Browse',command = browse_file).grid(row=0,column=1,pady=4)

Label_1 = tk.Label(master,text="The file selected: "+file_path).grid(row=1,column=0)

master.mainloop()
Block
  • 159
  • 1
  • 6