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, "/"))