0

I can't seem to figure out how to pass user_filepath global onto my second function. What can I change to do this? I'm using these two functions in a GUI. I try passing it to split_lines() but that isn't working saying user_filepath is not defined. Could I have some insight onto how to correct this? I'm new with tkinter and handling filepaths.

class Sequence_Class:
"This holds the functions for the button sequence."
global user_filepath

def open_file(): # Defines the function that opens the user's file in specific location.
    user_filepath = askopenfilename(filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")])
    if not user_filepath:
        return
    txt_edit.delete("1.0", tk.END)
    with open(user_filepath, mode="r", encoding="utf-8") as input_file:
        text = input_file.read()
        txt_edit.insert(tk.END, text)
    window.title(f"Linestring Compiler V1.0 - {user_filepath}")

# Defines the function that reads, delimits and quantifies the data.
def split_lines(user_filepath, delimiter, remove = '^[0-9.]+$'):
    for line in user_filepath:
        tokens = line.split(delimiter)
        tokens = [re.sub(remove, "", token) for token in tokens]
        clean_list = list(filter(lambda e:e.strip(), tokens))
    txt_edit.delete("1.0", tk.END)
    with open(user_filepath, mode="r", encoding="utf-8") as input_file:
        clean_list = input_file.read()
        txt_edit.insert(tk.END, clean_list)

def save_file(): # Defines the function that saves the open file to a new location.
    filepath = asksaveasfilename(defaultextension=".txt", filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")],)
    if not filepath:
        return
    with open(filepath, mode="w", encoding="utf-8") as output_file:
        text = txt_edit.get("1.0", tk.END)
        output_file.write(text)
...

btn_compile = tk.Button(frm_buttons, text="Compile Lines", command=Sequence_Class.split_lines(user_filepath, "/"))
3
  • you can use global keyword here. Commented May 10, 2022 at 17:46
  • How would I go about that? I tried, however I couldn't figure out how to call that global in my second function Commented May 10, 2022 at 17:47
  • The design of your functions needs a rework. You should either create a class with a file path as attribute, which makes it accessible to all methods, or your functions should take a file path as parameter and act on that. Commented May 10, 2022 at 17:53

1 Answer 1

1

Just define filepath at the global level.

filepath = askopenfilename(filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")])

def open_file():
    file_path = filepath
    if not filepath:
        return
    txt_edit.delete("1.0", tk.END)
    with open(filepath, mode="r", encoding="utf-8") as input_file:
        text = input_file.read()
        txt_edit.insert(tk.END, text)
    window.title(f"Linestring Compiler V1.0 - {filepath}")

lines = filepath
def split_lines(lines, delimiter, remove = '^[0-9.]+$'):
    for line in lines:
        tokens = line.split(delimiter)
        tokens = [re.sub(remove, "", token) for token in tokens]
        clean_list = list(filter(lambda e:e.strip(), tokens))
        cleaned_data.append(clean_list)
    txt_edit.delete("1.0", tk.END)
    with open(input_file, mode="r", encoding="utf-8") as input_file:
        text = input_file.read()
        txt_edit.insert(tk.END, text)

It looks like there are some redundant variables, like file_path and lines. You might want to open the file in the global scope and manipulate it in your functions instead of opening it in both functions.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, I tried including the global call for filepath, then use that in my other function split_lines() but i'm getting an error that filepath is not defined? Do I need to call lines = filepath in order to use my second function?
My apologies, this solution is wrong. Correcting now.
Hi @rangeseeker, will this work the same if I use the global within a class?
A class will be able to access a global variable.

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.