0

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

 
  • Please provide the expected [MRE](https://stackoverflow.com/help/minimal-reproducible-example). Show where the intermediate results deviate from the ones you expect. We should be able to paste a single block of your code into file, run it, and reproduce your problem. This also lets us test any suggestions in your context. There's nothing in your code that attempts to make the DF available outside the function; please supply the non-working example. – Prune Dec 04 '20 at 00:59
  • Before you do that, please make sure that you've looked up what `global` does; from the code presented here, I'm worried that you haven't done that yet. – Prune Dec 04 '20 at 01:00
  • [Include your data frame](https://stackoverflow.com/questions/52413246/how-to-provide-a-reproducible-copy-of-your-dataframe-with-to-clipboard) as part of the example. Don't expect us to edit a test file to support your code -- the input isn't part of your question. – Prune Dec 04 '20 at 01:01
  • 1
    If `getExcel()` is executed, `df` should be created in global space and can be accessed inside other functions. – acw1668 Dec 04 '20 at 01:14
  • Have you tried initializing `df` outside of the function? – Josmy Dec 04 '20 at 01:39
  • you have to define `df` outside the function. – Flavio Moraes Dec 04 '20 at 05:29
  • the global df variable is defined outside the function, but still the dataframe appears to be empty. – Nikos Choriatellis Dec 04 '20 at 08:14
  • I think you don't need to have the exact data frame file to test the code. It's just a matter of syntax in the python code. – Nikos Choriatellis Dec 04 '20 at 08:15

1 Answers1

0

When print(df.dtypes) is executed, no file is loaded yet and so it is empty. You should call it inside a function which should be called after a file is loaded:

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 = None

# browse file and import data
def getExcel():
    global df
    import_file_path = filedialog.askopenfilename()
    if import_file_path:
        df = pd.read_excel(import_file_path)
        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)

def showExcel():
    if df is not None:
        # a file is loaded
        print(df.dtypes)

tk.Button(root, text="Show File Data", width=15, height=2, command=showExcel).place(x=200, y=150)

root.mainloop()
acw1668
  • 40,144
  • 5
  • 22
  • 34
  • That's seriously better, thank you. But still I don't have access to the dataframe since it's still inside the function showExcel. That way I can't go on manipulating data to do stuff. – Nikos Choriatellis Dec 04 '20 at 10:57
  • You need to know that tkinter is using event-driven architecture. Tasks should be triggered by events. `showExcel()` is one of the example which triggered by a button click event. Therefore you need to think carefully how your program flows and how to access the required data (for example `df`) in a event-driven callback. – acw1668 Dec 04 '20 at 11:04
  • actually I did make it somehow, just by rewriting the syntax with no extra tkinter events, but then I changed something and can't get it back. somethting to do with the getExcel function. – Nikos Choriatellis Dec 04 '20 at 11:41
  • maybe I need a tikinter button that just continues with the rest of the code – Nikos Choriatellis Dec 04 '20 at 11:49