-1

The Code:

import pandas as pd
from tkinter import *
from tkinter import filedialog as fd

app = Tk()
file_path = ''
def set_file_path(path):
    global file_path
    file_path = path



def open_text_file():
    filetypes = (
        ('Excel files', '*.xlsx'),
        ('All files', '*.*')
    )
    file = fd.askopenfile(filetypes=filetypes)
    set_file_path(file)

def main(row, column, file):
    file = pd.read_excel(file)
    row_new = row - 1
    column_new = column - 1
    text = file[column_new][row_new]
    return text


app.title('XLSX Reader')
app.geometry("400x400")
open_button = Button(app, text='Choose File', command=open_text_file)
open_button.pack()
row = Entry(app)
row.pack()
column = Entry(app)
column.pack()
Submit = Button(app, text='Get Text', command=main(row=row,column=column,file=file_path))
Submit.pack()
output = main()
label = Label(text=output)
label.pack()

app.mainloop()

Error:

Traceback (most recent call last):
  File "C:\Users\91989\Documents\Arnav_Mann\Study\Science\Computer\Python\GraficalUserInterface\Excel_files\main.py", line 37, in <module>
    Submit = Button(app, text='Get Text', command=main(row=row,column=column,file=file_path))
  File "C:\Users\91989\Documents\Arnav_Mann\Study\Science\Computer\Python\GraficalUserInterface\Excel_files\main.py", line 22, in main
    file = pd.read_excel(file)
  File "C:\Users\91989\Documents\Arnav_Mann\Study\Science\Computer\Python\GraficalUserInterface\Excel_files\kernal\lib\site-packages\pandas\util\_decorators.py", line 211, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\91989\Documents\Arnav_Mann\Study\Science\Computer\Python\GraficalUserInterface\Excel_files\kernal\lib\site-packages\pandas\util\_decorators.py", line 331, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\91989\Documents\Arnav_Mann\Study\Science\Computer\Python\GraficalUserInterface\Excel_files\kernal\lib\site-packages\pandas\io\excel\_base.py", line 482, in read_excel
    io = ExcelFile(io, storage_options=storage_options, engine=engine)
  File "C:\Users\91989\Documents\Arnav_Mann\Study\Science\Computer\Python\GraficalUserInterface\Excel_files\kernal\lib\site-packages\pandas\io\excel\_base.py", line 1652, in __init__
    ext = inspect_excel_format(
  File "C:\Users\91989\Documents\Arnav_Mann\Study\Science\Computer\Python\GraficalUserInterface\Excel_files\kernal\lib\site-packages\pandas\io\excel\_base.py", line 1525, in inspect_excel_format
    with get_handle(
  File "C:\Users\91989\Documents\Arnav_Mann\Study\Science\Computer\Python\GraficalUserInterface\Excel_files\kernal\lib\site-packages\pandas\io\common.py", line 865, in get_handle
    handle = open(handle, ioargs.mode)
FileNotFoundError: [Errno 2] No such file or directory: ''

Process finished with exit code 1

I am trying to make a python application in Tkinter that allows the user to get a specific cell. But whenever I tried to run the code, I got the error that there is no file or directory: ''. I was expecting the code to provide a GUI that will allow the user to give the specific cell. Can anyone help.

Thingamabobs
  • 7,274
  • 5
  • 21
  • 54
  • 1
    You have an empty string as path. – Thingamabobs Dec 10 '22 at 13:28
  • 1
    `Submit = Button(app, text='Get Text', command=main(row=row,column=column,file=file_path)` and here you execute the command when you run your script and seems to be the root of your problem. – Thingamabobs Dec 10 '22 at 13:29
  • Does this answer your question? [Why is my Button's command executed immediately when I create the Button, and not when I click it?](https://stackoverflow.com/questions/5767228/why-is-my-buttons-command-executed-immediately-when-i-create-the-button-and-no) – Thingamabobs Dec 10 '22 at 13:29
  • I tried but the error is coming the same – Arnav Mann Dec 10 '22 at 13:35

1 Answers1

0

You need to change this line:

Submit = Button(app, text='Get Text', command=main)

So it won't be executed when declared. Then go ahead and alter your function to allow dynamic changes.

def main():
    open_text_file() #have a filepath
    f = pd.read_excel(file_path)
    row_new = int(row.get()) - 1 #get content of Entry
    column_new = int(column.get()) - 1 #get content of Entry
    text = f[column_new][row_new]
    #return text #you can't return to a button command
Thingamabobs
  • 7,274
  • 5
  • 21
  • 54