In the following code I get to select an excel file using a tkinter button. I'm using a function to read the excel file and import data and convert to dataframe. The problem is I don't have access to the dataframe outside the function although it's a global variable, therefore I can't continue with the rest of stuff.
I can now access the df variable, but not as a DataFrame which appears to be empty.
What is best to do to get around this ?
import tkinter as tk
from tkinter import filedialog, ttk
import pandas as pd
root=tk.Tk()
root.title("THIS IS MY FIRST APPLICATION")
root.geometry("600x300")
text_import = ("Choose your file: ")
df = pd.DataFrame()
# browse file and import data
def getExcel ():
global df
import_file_path = filedialog.askopenfilename()
data1 = pd.read_excel(import_file_path)
df = pd.DataFrame(data1)
print(df)
# create button
my_button = tk.Button(root, text = text_import, width = 15, height = 2,
command = getExcel)
my_button.pack(pady=10)
my_button.place(x = 200, y = 75)
print(df.dtypes)
root.mainloop()