0

I have two excel files to import; file1.xls and file2.xls. I have define two separate function to import and third function to merge these files. However, merge is not happening. The error is showing

NameError: Name 'File1' is not defined.

I am using below code. Please help.

from tkinter import messagebox, filedialog, Frame, Button, Tk, Menu
import pandas as pd

def importivl():
    global File1
    LI_Filepath = filedialog.askopenfilename()
    File1 = pd.read_excel(LI_Filepath)
    messagebox.showinfo("File1", "File Imported Successfully")

def importcatfile():
    global File2
    Cat_Filepath = filedialog.askopenfilename()
    File2 = pd.read_excel(Cat_Filepath)
    messagebox.showinfo("File2", "File Imported Successfully")

def mergeivlcat():
    File1 = pd.merge(File1, File2, on = "ID", how = "left")

def phase_one():
    frame = Frame(root)
    frame.pack()
    import_LI_File = Button(root, text = "Import File1", command = importivl).pack()
    import_Category_File = Button(root, text = "Import File2", command = importcatfile).pack()
    mergeivlcat()

root = Tk()
root.geometry("600x400")

menubar = Menu(root)
filemenu = Menu(menubar, tearoff = 0)
filemenu.add_command(label = "Phase One", command = phase_one)
filemenu.add_separator()
filemenu.add_command(label = "Exit", command = root.destroy)
menubar.add_cascade(label = "File", menu = filemenu)

root.config(menu = menubar)
root.mainloop()
ProteinGuy
  • 1,754
  • 2
  • 17
  • 33
Prafulla
  • 575
  • 1
  • 3
  • 21
  • 1
    Does this answer your question? [Using global variables in a function](https://stackoverflow.com/questions/423379/using-global-variables-in-a-function) – David Buck Jan 21 '20 at 11:25
  • 1
    Why not pass in `File1` and `File2` as arguments to the functions, rather than using them as global variables? – ProteinGuy Jan 21 '20 at 11:28

0 Answers0