Skip to content
Snippets Groups Projects

Sc 107/default extention file selection

Merged Ghost User requested to merge SC-107/default-extention-file-selection into master
2 files
+ 37
20
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 36
19
# The controller class
from tkinter.filedialog import *
import os
class ControllerClass:
"""Here the controller is located. Add all functionality as separate
@@ -14,10 +14,14 @@ class ControllerClass:
self.model = model
# Button bindings. Lambda is to pass argument to select_file
self.view.button_source.bind("<Button>", lambda x: self.select_file(1))
self.view.button_delete_source.bind("<Button>", lambda x: self.delete_file("source"))
self.view.button_shadow.bind("<Button>", lambda x: self.select_file(2))
self.view.button_delete_shadow.bind("<Button>", lambda x: self.delete_file("shadow"))
self.view.button_source.bind("<Button>", lambda x: self.select_file(
"source"))
self.view.button_delete_source.bind("<Button>", lambda x: self.
delete_file("source"))
self.view.button_shadow.bind("<Button>", lambda x: self.select_file(
"shadow"))
self.view.button_delete_shadow.bind("<Button>", lambda x: self.
delete_file("shadow"))
self.view.comparison_button.bind("<Button>",
lambda x: self.check_for_files())
self.view.save_button.bind("<Button>",
@@ -47,25 +51,38 @@ class ControllerClass:
def update_view(self):
self.view.window.after(1)
def select_file(self, fileno):
"""Function for selecting a file from the local computer. Assume .txt
upload format.
def select_file(self, file_type):
"""Function for selecting a file from the local computer. Assume .csv
or .txt upload format.
First it gets the file path and then save it in the model, then updates
view
view.
Args:
file_type: "source" for selecting source, "shadow" for shadow
"""
file_paths = askopenfilenames(filetypes=((".txt files", "*.txt"),
("all files", "*.*")))
# Extract the path to this project to use as intialdir for selection:
dirpath = os.getcwd()
basename = os.path.basename(dirpath)
datapath = dirpath.split(basename)[0] + "DataAndMatLabScript"
if file_type == "source":
datapath += r"\sourcefiles"
else:
datapath += r"\shadowedfiles"
file_paths = askopenfilenames(
initialdir=datapath, # If not found, then not a problem.
title="",
defaultextension='.csv',
filetypes=(("Comma-separated values", "*.csv"),
("TextGrid", "*.TextGrid"),
("Text files", "*.txt"),
("all files", "*.*"))
)
for file_path in file_paths:
if file_path != "": # Do not assign an empty file path
label_text = file_path.title().split("/")[-1]
if fileno == 1:
self.model.add_file(file_path, "source")
self.view.add_file_entry(label_text, "source")
else:
self.model.add_file(file_path, "shadow")
self.view.add_file_entry(label_text, "shadow")
self.model.add_file(file_path, file_type)
self.view.add_file_entry(label_text, file_type)
self.update_view()
# I find the naming here ill defined. Maybe it would be
Loading