def insert_char(text: tk.Text, char: str, raw: str = '', go=True): try: sel = text.get(tk.SEL_FIRST, tk.SEL_LAST) except tk.TclError: pass else: insert_text = raw + sel + char text.delete(tk.SEL_FIRST, tk.SEL_LAST) text.edit_separator() text.insert(tk.INSERT, insert_text) return 'break' index = str(text.index(tk.INSERT)).split('.') if text.get(f'{index[0]}.{int(index[1])}') == char: if char == raw: text.mark_set(tk.INSERT, f'{index[0]}.{int(index[1]) + 1}') text.see(tk.INSERT) return 'break' if raw: text.insert(tk.INSERT, raw) if (char != raw) or (char == '"') or char == "'": text.insert(tk.INSERT, char) if go: text.mark_set(tk.INSERT, f'{index[0]}.{int(index[1]) + 1}') text.see(tk.INSERT) return 'break'
def select_all(t: tk.Text) -> "break": """It's not necessary to protect this with @keepTextDisabled """ t.tag_add(tk.SEL, "1.0", tk.END) t.mark_set(tk.INSERT, "1.0") t.see(tk.INSERT) return "break"
def update(tktext: tk.Text, file: Path, parent=None) -> None: """ Replace text in open log window with (new) log file content. :param tktext: A tkinter.scrolledtext.ScrolledText or tkinter.Text insert. :param file: Path object of file from which to replace content. :param parent: The parent window over which to place messagebox; usually a Toplevel(). Defaults to app window. """ if not Path.exists(file): msg = (f'On {node()}, cannot update file:\n{file}\n' 'Was file deleted, moved or renamed?') messagebox.showerror(title='FILE NOT FOUND', detail=msg, parent=parent) return tktext.delete(tk.INSERT, tk.END) tktext.insert(tk.INSERT, Path(file).read_text()) tktext.see(tk.END) tktext.pack(fill=tk.BOTH, side=tk.LEFT, expand=True) # Need to remove focus from calling Button so can execute any # immediately following rt-click commands in parent. Use as a # precaution in case Button is not configured takefocus=False. if parent: parent.focus_set()
def console(text: tk.Text): """ Refreshes the logging console """ messages = rtm.get_logging_data() text.insert(tk.END, messages) if running: text.see(tk.END) text.after(INTERVAL, console, text)
def log(message, tex: tk.Text): # dont let console exceed 1 kB if (len(tex.get(1.0, tk.END).encode('utf-8')) > 1024): tex.delete(1.0, tk.END) tex.insert(tk.END, message) # insert message tex.see(tk.END) # scroll if necessary
def search(self, ax: Axes, start: Point, end: Point, output: tkinter.Text) -> float: output.delete('1.0', tkinter.END) pq: VertexSearchQueue[Tuple[float, HashVertex]] = VertexSearchQueue() is_goal_reached: bool = False vi = self.closest_vertex(start) vf = self.closest_vertex(end) visited: Set[HashVertex] = set() path: List[HashVertex] = [] cost_path: float = float('Inf') cost_table = [] vi.cost_h = self.h(vi, vf) output.insert(tkinter.END, '\n' + f'initial_h: {vi.cost_h}') output.insert(tkinter.END, '\n' + f'initial: {vi.i},{vi.j} ({vi.x},{vi.y})') output.insert(tkinter.END, '\n' + f'final: {vf.i},{vf.j} ({vf.x},{vf.y})') pq.put((vi.cost_h, vi)) while not pq.empty(): v: HashVertex cost_f, v = pq.get() if v == vf: cost_path = v.cost_g is_goal_reached = True path.append(v) while v.previous is not None: path.append(v.previous) if v == vi: break v = v.previous path.reverse() break visited.add(v) cost_neighbour_pairs = self.get_cost_neighbour_pairs(v) self.draw_path_attempt(ax=ax, vertex=v) for cost_neighbour_pair in cost_neighbour_pairs: (g, vn) = cost_neighbour_pair cost_h = self.h(vn, vf) cost_g = v.cost_g + g queue_vertices = pq.vertices() if vn not in visited and vn not in queue_vertices: vn.previous = v vn.cost_g = cost_g vn.cost_h = cost_h pq.put((vn.cost_f, vn)) elif vn in queue_vertices: (cost_fn_same, vn_same) = pq.find(vn) if cost_fn_same > cost_g + cost_h: vn.previous = v vn.cost_g = cost_g vn.cost_h = cost_h pq = pq.replace(to_remove=vn, to_put=(vn.cost_f, vn)) output.insert(tkinter.END, '\n' + 'is_goal_reached: ' + str(is_goal_reached)) if is_goal_reached: path = path[path.index(vi):] for i in range(1, len(path)): self.draw_lines(ax=ax, from_vertex=path[i - 1], to_vertices=[path[i]], color='red', linewidth=2) step_cost = path[i].cost_g - path[i - 1].cost_g h_prime = path[i].cost_h h = path[i - 1].cost_h step_cost_plus_h_prime = step_cost + h_prime h_star = cost_path - path[i - 1].cost_g h_str = '{:.2f}'.format(h) h_star_str = '{:.2f}'.format(h_star) step_cost_plus_h_prime_str = '{:.2f}'.format( step_cost_plus_h_prime) cost_table.append( f'h*(n)={h_star_str}, h(n)={h_str}, h(n)<=h*(n): {h <= h_star}, c(n,n\')+h(n\')={step_cost_plus_h_prime_str}, c(n,n\')+h(n\')>=h(n): {step_cost_plus_h_prime >= h}' ) for cost_element in cost_table: output.insert(tkinter.END, '\n' + cost_element) output.see(tkinter.END) return cost_path