0

I have this GUI that generates an Entry widget and a Button widget. The user is supposed to enter a file path (example "C:\Users\example\example") and that file path is used as the variable for the function LoopAnalysis().

When I execute the code, the widgets work and I can copy and paste a file path into the Entry widget, but when I execute the command using the Button widget I get the following error:

FileNotFoundError: [WinError 3] The system cannot find the path specified: ''

If the user inputs in the Entry window a file path, is there a specific syntax to use it as a variable? For example I know that for python in general if I manually define a variable as such.

Directory = r'C:\Users\example\example'

I need to add the r before the file path for python to recognize the entire line as a file path and not consider the special characters in the file path.

How do I do this for my function?

Torque_Analysis is already defined and works properly.

ws = Tk()
ws.title("Torque Analysis")
ws.geometry('1000x150')
ws['bg'] = 'black'

def LoopAnalysis(Directory):
    for filename in os.listdir(Directory):
        if filename.endswith(".csv"):
            Torque_Analysis(os.path.join(Directory, filename))
            plt.figure()
            plt.show(block = False)
        else: continue

UserEntry = Entry(ws, width = 150)
UserEntry.pack(pady=30)
Directory = UserEntry.get()

Button(ws,text="Click Here to Generate Curves", padx=10, pady=5, command=lambda: LoopAnalysis(Directory)).pack()

ws.mainloop()
2
  • 1
    You're calling UserEntry.get() about a millisecond after you create the entry widget, so it's going to return an empty string. Commented Jun 29, 2021 at 18:45
  • To avoid that, create a new function that calls UserEntry.get() and then calls LoopAnalysis(). Afterwards, make this new function the command= of the "Click Here" button. Commented Jun 29, 2021 at 18:54

2 Answers 2

1

You are calling the get method immediately after creating the entry widget, so it's going to return an empty string. In GUI programming, you need to get data from widgets at the point that you need the data, not before.

My recommendation is to not pass in the value to the function. Instead, have the function call get:

Button(..., command=LoopAnalysis)
...
def LoopAnalysis():
    Directory = UserEntry.get()
    ...
Sign up to request clarification or add additional context in comments.

Comments

0

As stated in a comment to the question, you are calling UserEntry.get() once, as soon as the program starts. The entry box is blank then, so that blank string is read and stored in Directory, and the contents of Directory never changes again.

Instead, you need to call UserEntry.get() every time the button is pressed.

Try this:

from tkinter import *
import os

ws = Tk()
ws.title("Torque Analysis")
ws.geometry('1000x150')
ws['bg'] = 'black'

def LoopAnalysis(Directory):
    for filename in os.listdir(Directory):
        if filename.endswith(".csv"):
            Torque_Analysis(os.path.join(Directory, filename))
            plt.figure()
            plt.show(block = False)
        else: continue

UserEntry = Entry(ws, width = 150)
UserEntry.pack(pady=30)
Button(ws,text="Click Here to Generate Curves", padx=10, pady=5, command=lambda: LoopAnalysis(UserEntry.get())).pack()

ws.mainloop()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.