class Tk_Table(Frame, object): def __init__(self, master, columns, data=None, command=None, editable=True, sort=True, select_mode=None, autoscroll=True, vscrollbar=True, hscrollbar=False, heading_anchor = CENTER, cell_anchor=W, style=None, scrollbar_background=None, scrollbar_troughcolor=None, height=None, padding=None, adjust_heading_to_content=False, stripped_rows=None, selection_background=None, selection_foreground=None, cell_background=None, cell_foreground=None, cell_font=None, field_background=None, heading_font= None, heading_background=None, heading_foreground=None, cell_pady=2, column_header=True, row_numbers=True, entry_background="#d6d6d6", entry_foreground=None, entry_validatecommand=None, entry_selectbackground="#1BA1E2", entry_selectborderwidth=None, entry_selectforeground=None, entry_font = "TkDefaultFont", rowlabel_anchor=E, rowlabel_minwidth=0, rowlabel_hoverbackground="#FFFFFF",frame_relief=None, frame_borderwidth=None, frame_background=None): frame_kwargs = {} if frame_relief is not None: frame_kwargs["relief"] = frame_relief if frame_borderwidth is not None: frame_kwargs["borderwidth"] = frame_borderwidth if frame_background is not None: frame_kwargs["background"] = frame_background Frame.__init__(self, master, class_="Multicolumn_Listbox", **frame_kwargs) self.grid_rowconfigure(0, weight=1) self.grid_columnconfigure(1, weight=1) self._multicolumn_listbox = Multicolumn_Listbox(self, columns, data=data, command=command, sort=sort, select_mode=select_mode, heading_anchor = heading_anchor, cell_anchor=cell_anchor, style=style, height=height, padding=padding, adjust_heading_to_content=adjust_heading_to_content, stripped_rows=stripped_rows, selection_background=selection_background, selection_foreground=selection_foreground, cell_background=cell_background, cell_foreground=cell_foreground, cell_font=cell_font, field_background=field_background, heading_font=heading_font, heading_background=heading_background, heading_foreground=heading_foreground, cell_pady=cell_pady, headers=column_header) self._multicolumn_listbox.interior.grid(row=0, column=1, sticky= N+E+W+S) self._mousewheel_detection = True if row_numbers: self._row_numbers = Row_Header(self, font=self._multicolumn_listbox.font, row_height=self._multicolumn_listbox.row_height, row_minwidth=rowlabel_minwidth, hover_background = rowlabel_hoverbackground, anchor=rowlabel_anchor, onclick=self._on_click_row_label) self._row_numbers.grid(row=0, column=0, sticky= N+S+E) self._multicolumn_listbox.interior.bind("<Map>", self._place_vertically_row_numbers) else: self._row_numbers = None if editable: self._selected_cell = None self._entry_popup = None self._multicolumn_listbox.interior.bind("<1>", self._edit_cell) def configure(event): """ if self._entry_popup: self._entry_popup.destroy() return """ self._multicolumn_listbox.interior.update_idletasks() self._update_position_of_entry() self._multicolumn_listbox.interior.bind("<Configure>", configure) self._entry_kwargs = entry_kwargs = {} if entry_background is not None: entry_kwargs["background"] = entry_background if entry_foreground is not None: entry_kwargs["foreground"] = entry_foreground if entry_validatecommand is not None: entry_kwargs["validatecommand"] = entry_validatecommand if entry_selectbackground is not None: entry_kwargs["selectbackground"] = entry_selectbackground if entry_selectforeground is not None: entry_kwargs["selectforeground"] = entry_selectforeground if entry_font is not None: entry_kwargs["font"] = entry_font if command is not None: self._command = command self._multicolumn_listbox.interior.bind("<<TreeviewSelect>>", self._on_select) scrollbar_kwargs = {} if scrollbar_background is not None: scrollbar_kwargs["background"] = scrollbar_background if scrollbar_troughcolor is not None: scrollbar_kwargs["throughcolor"] = scrollbar_troughcolor if vscrollbar: if editable: if row_numbers: def yview_command(*args): self._multicolumn_listbox.interior.yview(*args) self._row_numbers.yview(*args) self._update_position_of_entry() else: def yview_command(*args): self._multicolumn_listbox.interior.yview(*args) self._update_position_of_entry() else: if row_numbers: def yview_command(*args): self._multicolumn_listbox.interior.yview(*args) self._row_numbers.yview(*args) else: yview_command = self._multicolumn_listbox.interior.yview self._vbar=Scrollbar(self,takefocus=0, command=yview_command, **scrollbar_kwargs) self._vbar.grid(row=0, column=2, sticky= N+S) def yscrollcommand(first,last): first, last = float(first), float(last) if first <= 0 and last >= 1: if self._mousewheel_detection: if autoscroll: self._vbar.grid_remove() if row_numbers: unbind_function_onMouseWheel(self._multicolumn_listbox.interior) self._mousewheel_detection = False else: if not self._mousewheel_detection: if autoscroll: self._vbar.grid() if row_numbers: bind_function_onMouseWheel(self._row_numbers, "y", binding_widget=self._multicolumn_listbox.interior, unit="pages") self._mousewheel_detection = True self._vbar.set(first, last) if editable: self._update_position_of_entry() self._multicolumn_listbox.interior.config(yscrollcommand=yscrollcommand) if hscrollbar: if editable: def xview_command(*args): self._multicolumn_listbox.interior.xview(*args) self._update_position_of_entry() else: xview_command = self._multicolumn_listbox.interior.xview self._hbar=Scrollbar(self,takefocus=0, command=xview_command, **scrollbar_kwargs) self._hbar.grid(row=1, column=1, sticky= E+W) if autoscroll: if editable: def xscrollcommand(f,l, self=self): make_autoscroll(self._hbar, f, l) self._update_position_of_entry() else: def xscrollcommand(f,l, hbar=self._hbar): make_autoscroll(hbar, f, l) self._multicolumn_listbox.interior.config(xscrollcommand=xscrollcommand) else: self._multicolumn_listbox.interior.config(xscrollcommand=self._hbar.set) def _place_vertically_row_numbers(self, event): self._multicolumn_listbox.interior.unbind("<Map>") item_ID = self._multicolumn_listbox.interior.insert('', 0, values=[""]*self._multicolumn_listbox.number_of_columns) self._multicolumn_listbox.interior.update() x,y,w,h = self._multicolumn_listbox.interior.bbox(item_ID) self._multicolumn_listbox.interior.delete(item_ID) self._row_numbers.grid_configure(pady=(y,0)) def _edit_cell(self, event): '''Executed, when a row is clicked. Opens an entry popup above the cell, so it is possible to select text ''' # close previous popups if self._entry_popup: self._destroy_entry() # what row and column was clicked on item_ID = self._multicolumn_listbox.interior.identify_row(event.y) if not item_ID: return column = self._multicolumn_listbox.interior.identify_column(event.x) if column == "": return # get column position info x,y,width,height = self._multicolumn_listbox.interior.bbox(item_ID, column) # place Entry popup properly column_number = int(column[1:])-1 cell_data = self._multicolumn_listbox.item_ID_to_row_data(item_ID)[column_number] self._entry_popup = Entry(self._multicolumn_listbox.interior, exportselection=True, borderwidth=0, **self._entry_kwargs) self._entry_popup.place(x=x, y=y, width=width, height=height) self._entry_popup.insert(0, cell_data) self._entry_popup.focus_force() self._entry_popup.bind("<Control-a>", lambda event: self._select_all_entry_data) self._entry_popup.bind("<Escape>", lambda event: self._destroy_entry()) self._entry_popup.bind("<FocusOut>", lambda event: self._destroy_entry()) bind_function_onMouseWheel(self._multicolumn_listbox.interior, "y", binding_widget=self._entry_popup, callback=self._update_position_of_entry, unit="pages") if self._row_numbers: bind_function_onMouseWheel(self._row_numbers, "y", binding_widget=self._entry_popup, unit="pages") self._entry_popup.bind("<Return>", self._on_update_cell) self._selected_cell = item_ID, column, column_number def _on_click_row_label(self, index): if self._selected_cell and self._multicolumn_listbox.item_ID(index) == self._selected_cell[0]: self._destroy_entry() self._multicolumn_listbox.toogle_selection(index) def _select_all_entry_data(self): ''' Set selection on the whole text ''' self._entry_popup.selection_range(0, 'end') # returns 'break' to interrupt default key-bindings return 'break' def _destroy_entry(self): self._entry_popup.destroy() self._entry_popup = None self._selected_cell = None def _on_update_cell(self, event): item_ID, column, column_number = self._selected_cell data = self._entry_popup.get() row_data = self._multicolumn_listbox.item_ID_to_row_data(item_ID) row_data[column_number] = data self._multicolumn_listbox.interior.item(item_ID, values=row_data) self._destroy_entry() def _update_position_of_entry(self): if self._selected_cell: bbox = self._multicolumn_listbox.interior.bbox(self._selected_cell[0], self._selected_cell[1]) if bbox == "": self._entry_popup.place_forget() else: x,y,width,height = bbox self._entry_popup.place(x=x, y=y, width=width, height=height) def configure_column(self, index, width=None, minwidth=None, anchor=None, stretch=None): self._multicolumn_listbox.configure_column(self, index, width=None, minwidth=None, anchor=None, stretch=None) def row_data(self, index): return self._multicolumn_listbox.row_data(index) def update_row(self, index, data): self._multicolumn_listbox.update_row(index,data) def delete_row(self, index): self._multicolumn_listbox.delete_row(index) if self._row_numbers: self._row_numbers.pop() def insert_row(self, data, index=None): self._multicolumn_listbox.insert_row(data, index) if self._row_numbers: self._row_numbers.new_label() def column_data(self, index): return self._multicolumn_listbox.column_data(index) def update_column(self, index, data): self._multicolumn_listbox.update_column(index, data) def clear(self): self._multicolumn_listbox.clear() if self._row_numbers: self._row_numbers.delete_labels() def update(self, data): current_number_of_rows = self._multicolumn_listbox.number_of_rows self._multicolumn_listbox.update(data) if self._row_numbers: number_of_rows = len(data) if current_number_of_rows < number_of_rows: for i in range(number_of_rows - current_number_of_rows): self._row_numbers.new_label() else: n_labels = current_number_of_rows - number_of_rows self._row_numbers.pop(n_labels =n_labels) def focus(self, index=None): self._multicolumn_listbox.focus(index) def state(self, state=None): self._multicolumn_listbox.state(state) @property def number_of_rows(self): return self._multicolumn_listbox.number_of_rows @property def number_of_columns(self): return self._multicolumn_listbox.number_of_columns def toogle_selection(self, index): self._multicolumn_listbox.toogle_selection(index) def select_row(self, index): self._multicolumn_listbox.select_row(index) def deselect_row(self, index): self._multicolumn_listbox.deselect_row(index) def deselect_all(self): self._multicolumn_listbox.deselect_all() def set_selection(self, indices): self._multicolumn_listbox.set_selection(indices) @property def selected_rows(self): return self._multicolumn_listbox.selected_rows @property def indices_of_selected_rows(self): return self._multicolumn_listbox.indices_of_selected_rows def delete_all_selected_rows(self): number_of_deleted_rows = self._multicolumn_listbox.delete_all_selected_rows() if self._row_numbers: self._row_numbers.pop(n_labels=number_of_deleted_rows) @property def table_data(self): return self._multicolumn_listbox.table_data @table_data.setter def table_data(self, data): self.update(data) def cell_data(self, row, column): return self._multicolumn_listbox.cell_data(row, column) def update_cell(self, row, column, value): self._multicolumn_listbox.update_cell(row, column, value) def __getitem__(self, index): return self._multicolumn_listbox[index] def __setitem__(self, index, value): self._multicolumn_listbox[index] = value def bind(self, event, handler): self._multicolumn_listbox.bind(event, handler) def sort_by(self, col, descending): self._multicolumn_listbox.sort_by(col, descending)
class A_DWI: def __init__(self, container, frame, label='', text='', row=0, column=0): self.container = container self.is_b0 = BooleanVar(container.parent) self.is_dw = BooleanVar(container.parent) self.column = column self.direction = StringVar(container.parent) self.label_from = Label(frame, text='from') self.text_from = Entry(frame) self.text_from.insert(0, text) self.button_file_from = Button(frame, text='...', command=lambda:filenameDialog_text(self.text_from)) self.button_rm = Button(frame, text='remove', command=self.click_remove) self.radio_ap = Radiobutton(frame, text='AP', variable=self.direction, value='AP', command=self.set_direction) self.radio_pa = Radiobutton(frame, text='PA', variable=self.direction, value='PA', command=self.set_direction) self.label_to = Label(frame, text='to') self.text_to = Entry(frame) #self.text_to.insert(0, text) self.button_file_to = Button(frame, text='Gen', command=self.set_filename_to) self.check_b0 = Checkbutton(frame, text='B0', variable=self.is_b0) self.check_dw = Checkbutton(frame, text='DWI', variable=self.is_dw) self.button_up = Button(frame, text='up', width=3, command=self.click_up) self.button_dn = Button(frame, text='down', width=3, command=self.click_dn) self.row = -1 self.change_row(row) if text != '': self.set_appa() self.set_filename_to() def prefix(self): return self.container.parent.prefix() def set_direction(self): pass def get_dwi_filenames(self): ''' :return: [('from', 'to'), ('from', 'to')] ''' filename_from = self.text_from.get() filename_to = self.text_to.get() if self.is_dw.get(): rtn = [ [filename_from, filename_to] ] filename_b_from = filename_wo_ext(filename_from) filename_b_to = filename_wo_ext(filename_to) rtn.append( [filename_b_from+'.bval', filename_b_to+'.bval'] ) rtn.append( [filename_b_from+'.bvec', filename_b_to+'.bvec'] ) return rtn return [] def get_b0_filename(self): ''' :return: [('from', 'to')] ''' filename_from = self.text_from.get() filename_to = self.text_to.get() ext = extname(filename_to) if self.is_b0.get(): if self.is_dw.get(): filename_to = '%s_B0%s' % (filename_wo_ext(filename_to), ext) return [ [filename_from, filename_to] ] return [] def set_appa(self): filename_from = self.text_from.get() basename_from = os.path.basename(filename_from) basename_b_from = filename_wo_ext(basename_from) if 'pa' in basename_b_from.lower(): self.direction.set('PA') elif 'ap' in basename_b_from.lower(): self.direction.set('AP') else: pass def set_filename_to(self, middle=None): filename_from = self.text_from.get() basename_from = os.path.basename(filename_from) number = os.path.dirname(filename_from).split('/')[-1].split('_')[0] if number == '': number = str(self.row) else: try: int(number) except: number = str(self.row) ext = extname(basename_from) intermediate = self.direction.get() if intermediate == '': intermediate = 'DWI' if self.is_b0.get() and not self.is_dw.get(): intermediate += '_B0' self.text_to.delete(0, len(self.text_to.get())) self.text_to.insert(0, '%s%s_%s%s' % (self.prefix(), number, intermediate, ext)) def change_row(self, row): if self.row == row: return self.row = row i = 2*row j = self.column j += 0; self.button_up.grid(row=i, column=j) j += 1; self.label_from.grid(row=i, column=j) j += 1; self.text_from.grid(row=i, column=j, sticky=EW) j += 1; self.button_file_from.grid(row=i, column=j) j += 1; self.button_rm.grid(row=i, column=j) j += 1; self.radio_ap.grid(row=i, column=j) j += 1; self.radio_pa.grid(row=i, column=j) i += 1 j = 0 j += 0; self.button_dn.grid(row=i, column=j) j += 1; self.label_to.grid(row=i, column=j) j += 1; self.text_to.grid(row=i, column=j, sticky=EW) j += 1; self.button_file_to.grid(row=i, column=j) j += 1 j += 1; self.check_b0.grid(row=i, column=j) j += 1; self.check_dw.grid(row=i, column=j) def click_remove(self): self.container.remove(self.row) self.button_up.destroy() self.label_from.destroy() self.text_from.destroy() self.button_file_from.destroy() self.button_rm.destroy() self.radio_ap.destroy() self.radio_pa.destroy() self.button_dn.destroy() self.label_to.destroy() self.text_to.destroy() self.button_file_to.destroy() self.check_b0.destroy() self.check_dw.destroy() def click_up(self): self.container.up(self.row) def click_dn(self): self.container.dn(self.row)
class Interface(): def __init__(self, init, speed): self.init_compt = init self.speed = speed self.window = Tk() self.title = self.window.title('IA Minoïde') self.window.geometry('500x200') self.lbl = Label(self.window) self.btn = Button(self.window) self.reinit() self.window.mainloop() def start(self): net = self.NETWORK.get() chk1 = self.chk_state1.get() try: value0 = int(self.txt1.get()) value1 = int(self.txt2.get()) value2 = int(self.txt3.get()) self.terget = [value0, value1, value2] except: #Messagebox.showinfo('Attention/', #'Les valeurs entrées ne sont pas correctes !') return self.txt1.destroy() self.txt2.destroy() self.txt3.destroy() self.chk0.destroy() self.chk1.destroy() self.res = 'Coordonnées cible: ('\ + str(value0) + ', ' + str(value1)\ +", "+ str(value2) + ')' #robot = VrepPioneerSimulation() robot = Pioneer(rospy) HL_size = 10 # nbre neurons of Hiden layer network = NN(3, HL_size, 2) self.trainer = OnlineTrainer(robot, network) if net: with open('last_w.json') as fp: json_obj = json.load(fp) for i in range(3): for j in range(HL_size): network.wi[i][j] = json_obj["input_weights"][i][j] for i in range(HL_size): for j in range(2): network.wo[i][j] = json_obj["output_weights"][i][j] print('Check 0 True') else: print('Check 0 False') if chk1: print('Check 1 True') thread = threading.Thread(target=self.trainer.train, args=(self.terget, )) thread.start() else: print('Check 1 False') if net: self.window.after(1, self.loop) else: self.window.after(1, self.save_file) def save_file(self): self.txt = Entry(self.window, width=30) self.lbl.config(text='Give a name to the log file (default to last):') self.btn.config(text='Valider', command=self.loop) self.txt.grid(column=0, row=1) self.lbl.grid(column=0, row=0) self.btn.grid(column=0, row=2) try: self.file_name = self.txt.get() except: """Messagebox.showinfo('Attention/', "Le nom de fichier n'est pas correct")""" if self.file_name == '': self.file_name = 'last' with open("logs/" + self.file_name + ".json", 'w') as f: print(self.trainer.log_file) json.dump(self.trainer.log_file, f) f.close() print('Graph values have been saved in ' + self.file_name + '.json file') def loop(self): self.lbl.config(text=self.res) self.btn.config(text='Stop', command=self.stop) try: self.txt.destroy() except: pass if self.arret == 1: self.arret = 0 self.lbl.config(text='Arrêt') self.btn.config(text='Réinitialiser', command=self.reinit) return if self.compt <= 0: self.lbl.config(text='Time is over !') self.btn.config(text='Réinitialiser', command=self.reinit) return self.compt -= self.speed / 100 self.window.after(1, self.loop) def stop(self): self.arret = 1 def reinit(self): self.arret = 0 self.compt = self.init_compt self.lbl.config(text='Saisissez les coordonnées:', bg="blue", fg="white", width=20) self.lbl.grid(column=0, row=0) self.txt1Lbl = Label(self.window) self.txt1 = Entry(self.window, width=3) self.txt2 = Entry(self.window, width=3) self.txt3 = Entry(self.window, width=3) self.txt1Lbl.config(text='X', bg="red", fg="white", width=3) self.txt1Lbl.grid(column=0, row=1) #self.lbl.pack() #self.txt1Lbl.pack() self.txt1.grid(column=1, row=1) self.txt2.grid(column=1, row=2) self.txt3.grid(column=1, row=3) self.NETWORK = BooleanVar() self.chk_state1 = BooleanVar() self.NETWORK.set(False) self.chk_state1.set(False) #set check state self.chk0 = Checkbutton(self.window, text='Load previous network', var=self.NETWORK) self.chk1 = Checkbutton(self.window, text='Learn', var=self.chk_state1) self.chk0.grid(column=2, row=1) self.chk1.grid(column=2, row=2) self.btn.config(text='Lancer', command=self.start) self.btn.grid(column=0, row=4)