Home > database >  Python[Pandas,Tkinter] Can't read excel file path from askopenfilename
Python[Pandas,Tkinter] Can't read excel file path from askopenfilename

Time:09-05

i try to get the excel path from Tkinter with askopenfilename. After the get path and read excel by pandas. found error

Exception in Tkinter callback 
Traceback (most recent call last):  
   File  "C:\Users\Pe\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
     return self.func(*args)
   File C:/Users/Pe/AppData/Local/Programs/Python/Python310/Project/GUI.py", line 23, in select_file
     read_excelr()
   File "C:/Users/Pe/AppData/Local/Programs/Python/Python310/Project/GUI.py", line 26, in read_excelr
     df=Pds.read_excel(xlsx) NameError: name 'xlsx' is not defined

could you help me for the method to read excel? thx

import tkinter as tk
from tkinter import ttk
from tkinter import filedialog as fd
from tkinter.messagebox import showinfo
import pandas as Pds

##### create the root window
root = tk.Tk()
root.title('Read Excel')
root.resizable(False, False)
root.geometry('750x150')

lbl1 = tk.Label(root, text="Input Excel File")
lbl1.grid(column=0,row=0)

def select_file():
    filetypes = (('All files', '*.xlsx'),('text files', '*.txt'))
    filename = fd.askopenfilename(title='Open a file',initialdir='/',filetypes=filetypes)
    showinfo(title='Selected File',message=filename)
    lbl2['text']=(filename)
    print(filename)
    xlsx = Pds.ExcelFile(filename)
    read_excelr()
    
def read_excelr():
    df=Pds.read_excel(xlsx)
    print(df)
##### open button
open_button = ttk.Button(root,text='Open a File',command=select_file)
open_button.grid(column=1, row=0)
##### Text edit
lbl2 = tk.Label(root, text='None')
lbl2.grid(column=2,row=0)

##### run the application
root.mainloop()

CodePudding user response:

Because "xlsx" is just a local variable, it only works in the specific function. You can pass xlsx to read_excelr function. This is the modified code:

import tkinter as tk
from tkinter import ttk
from tkinter import filedialog as fd
from tkinter.messagebox import showinfo
import pandas as Pds

##### create the root window
root = tk.Tk()
root.title('Read Excel')
root.resizable(False, False)
root.geometry('750x150')

lbl1 = tk.Label(root, text="Input Excel File")
lbl1.grid(column=0,row=0)

def select_file():
    filetypes = (('All files', '*.xlsx'),('text files', '*.txt'))
    filename = fd.askopenfilename(title='Open a file',initialdir='/',filetypes=filetypes)
    showinfo(title='Selected File',message=filename)
    lbl2['text']=(filename)
    print(filename)
    xlsx = Pds.ExcelFile(filename)
    read_excelr(xlsx)
    
def read_excelr(xlsx):
    df=Pds.read_excel(xlsx)
    print(df)
##### open button
open_button = ttk.Button(root,text='Open a File',command=select_file)
open_button.grid(column=1, row=0)
##### Text edit
lbl2 = tk.Label(root, text='None')
lbl2.grid(column=2,row=0)

##### run the application
root.mainloop()
  • Related