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, "/"))
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.