import tkinter as tk import tkinter.ttk as ttk from tkinter import filedialog import validators class StatusPanel(tk.Frame): def __init__(self, parent): tk.Frame.__init__(self, parent.master) self._parent = parent self.label = tk.Label(self, bd=1, relief=tk.SUNKEN, anchor=tk.S) self.label.pack(fill=tk.X) self.pack(side=tk.BOTTOM, fill=tk.X) def set_text(self, text): self.label.config(text=text) self.label.update_idletasks() def clear_text(self): self.set_text('') class FilePanel(tk.Frame): def __init__(self, parent): tk.Frame.__init__(self, parent.master) self._parent = parent self.btn_files = tk.Button(self, text="Select Files", command=self._parent.select_files) self.btn_files.pack(pady=5, padx=5) self.label = tk.Label(self, anchor=tk.SW) self.label.pack(fill=tk.X, pady=5, padx=5) ttk.Separator(self, orient=tk.HORIZONTAL).pack(side=tk.BOTTOM, fill=tk.X) self.pack(side=tk.TOP, fill=tk.X) def set_text(self, text): self.label.config(text=text) self.label.update_idletasks() def clear_text(self): self.set_text('') class FieldPanel(tk.Frame): def __init__(self, parent): tk.Frame.__init__(self, parent.master) self._parent = parent self.is_single = tk.BooleanVar() self.is_single.set(True) self.rbtn_single = tk.Radiobutton(self, text='single excel files', variable=self.is_single, value=True, command=self.disable_listbox) self.rbtn_combine = tk.Radiobutton(self, text='one combined excel file', variable=self.is_single, value=False, command=self.enable_listbox) self.rbtn_single.pack(anchor=tk.W) self.rbtn_combine.pack(anchor=tk.W) self.listbox = tk.Listbox(self, selectmode=tk.MULTIPLE, activestyle='none') self.listbox.pack(fill=tk.X, ipady=5, ipadx=5) self.pack(side=tk.TOP, fill=tk.X) def disable_radio_btn(self): self.rbtn_single.config(state="disabled") self.rbtn_combine.config(state="disabled") def disable_listbox(self): self.listbox.config(state="disabled") def disable(self): self.disable_listbox() self.disable_radio_btn() def enable_radio_btn(self): self.rbtn_single.config(state="normal") self.rbtn_combine.config(state="normal") self.is_single.set(True) def enable_listbox(self): self.listbox.config(state="normal") def enable(self): self.enable_listbox() self.enable_radio_btn() def set_listbox_content(self, choices, selection): self.enable_listbox() current = self.listbox.size() self.listbox.selection_clear(0, current) self.listbox.delete(0,current) for i, element in enumerate(choices): self.listbox.insert(tk.END, element) if element in selection: self.listbox.selection_set(i) self.disable_listbox() class ActionPanel(tk.Frame): def __init__(self, parent): tk.Frame.__init__(self, parent.master) self._parent = parent self.btn_go = tk.Button(self, text="GO!", command=self._parent.quit) self.btn_go.pack(side=tk.LEFT, pady=5, padx=5) self.btn_quit = tk.Button(self, text="Quit", command=self._parent.quit) self.btn_quit.pack(side=tk.RIGHT, pady=5, padx=5) self.pack(side=tk.BOTTOM, fill=tk.X) def disable(self): self.btn_go.config(state='disabled') def enable(self): self.btn_go.config(state='active') class Application(tk.Frame): def __init__(self, master): master.minsize(height=330, width=200) tk.Frame.__init__(self, master) self._master = master self.pack(fill=tk.BOTH) self.status_panel = StatusPanel(self) self.file_panel = FilePanel(self) self.field_panel = FieldPanel(self) self.action_panel = ActionPanel(self) self.reset() def reset(self): self.field_panel.disable() self.action_panel.disable() self.file_validator = None self.validated_files = None def select_files(self): self.reset() self.file_panel.clear_text() opts = { 'initialdir': '~/_signalyse test', 'filetypes': [('Signalyse Statistics', '.stx'), ('Signalyse Data', '.dat'), ('Sensovation Data', '.csv')], 'multiple': True} file_selection = tk.filedialog.askopenfilename(**opts) if not isinstance(file_selection, tuple) or len(file_selection) == 0: return self.file_validator = validators.guess_validator(file_selection) if self.file_validator is None: return self.validated_files = [f for f in validators.validate_files(file_selection, self.file_validator)] status = '%d of %d valid %s files found' % (len(self.validated_files), len(file_selection), self.file_validator.type) self.file_panel.set_text(status) if len(self.validated_files) == 0: self.reset() return self.field_panel.enable_radio_btn() self.field_panel.set_listbox_content(self.file_validator.data_fields, self.file_validator.defaults) def quit(self): self._master.destroy()