def set_mode(self, new_state): assert new_state in self.modes self.mode = new_state self.buttons['Edit'].config( text="Write" if self.in_edit_mode else self.button_name("Edit")) self.clear_story_frame() if self.mode == "Read": self.textbox_frame.pack(expand=True, side="top", fill='both') self.textbox.config(foreground=text_color(), background=bg_color()) self.textbox.edit_reset() elif self.mode == "Edit": self.textbox_frame.pack(expand=True, side="top", fill='both') self.textbox.config(foreground=text_color(), background=edit_color()) #self.secondary_textbox_frame.pack(expand=False, side="bottom", fill='both') #self.secondary_textbox.config(foreground=text_color(), background=edit_color()) #self.preview_textbox.config(foreground=text_color(), background=edit_color()) elif self.mode == "Visualize": self.vis.frame.pack(side=tk.LEFT, expand=True, fill=tk.BOTH) # elif self.mode == "Multiverse": # self.multiverse.frame.pack(side=tk.LEFT, expand=True, fill=tk.BOTH) else: raise NotImplementedError(self.mode, type(self.mode))
def set_mode(self, new_state): assert new_state in self.modes self.mode = new_state self.edit_button.config(text="Finish Editing" if self. in_edit_mode else self.button_name("Edit")) self.clear_story_frame() if self.mode == "Read": self.textbox_frame.pack(expand=True, side="top", fill='both') self.textbox.config(foreground=text_color(), background=bg_color()) self.textbox.edit_reset() elif self.mode == "Edit": self.textbox_frame.pack(expand=True, side="top", fill='both') self.secondary_textbox_frame.pack(expand=False, side="bottom", fill='both') self.textbox.config(foreground=text_color(), background=edit_color()) self.secondary_textbox.config(foreground=text_color(), background=edit_color()) # Caller needs to use start_multi_edit elif self.mode == "Multi Edit": pass elif self.mode == "Visualize": self.vis.frame.pack(side=tk.LEFT, expand=True, fill=tk.BOTH) else: raise NotImplementedError(self.mode, type(self.mode))
def setup_textbox_tags(self, textbox): #textbox.tag_configure("bold", font=self.font_bold) textbox.tag_configure("node_select", background=edit_color()) textbox.tag_configure("modified", background="blue", foreground=text_color()) textbox.tag_configure('match', background='blue', foreground=text_color()) textbox.tag_configure('active_match', background='black', foreground='white') textbox.tag_raise("sel") textbox.tag_raise("insert")
def body(self, master): self.memory_textbox = ScrolledText(master, height=3) self.memory_textbox.grid(row=master.grid_size()[1], column=0, columnspan=2) self.memory_textbox.configure( font=Font( family="Georgia", size=12), # Other nice options: Helvetica, Arial, Georgia spacing1=10, foreground=text_color(), background=bg_color(), padx=3, pady=3, spacing2=3, # Spacing between lines spacing3=5, wrap="word", ) self.memory_textbox.insert(tk.INSERT, self.memory['text']) self.memory_textbox.focus() row = master.grid_size()[1] create_side_label(master, "Inheritability", row) inheritability_options = ('none', 'subtree', 'delayed') self.inheritability.set(self.memory['inheritability']) dropdown = tk.OptionMenu(master, self.inheritability, *inheritability_options) dropdown.grid(row=row, column=1, pady=3) self.delete_button = create_button(master, "Delete memory", self.delete_memory, width=15)
def start_multi_edit(self, num_textboxes=5): assert self.mode == "Multi Edit" self.clear_story_frame() self.multi_edit_frame = ScrollableFrame(self.story_frame) self.multi_edit_frame.pack(expand=True, fill="both") self.multi_textboxes = [ TextAware(self.multi_edit_frame.scrollable_frame, height=5) for i in range(num_textboxes) ] for tb in self.multi_textboxes: tb.pack(expand=True, fill="both") readable_font = Font( family="Georgia", size=12) # Other nice options: Helvetica, Arial, Georgia tb.configure( font=readable_font, spacing1=10, foreground=text_color(), # Darkmode background=bg_color(), padx=2, pady=2, # spacing2=3, # Spacing between lines4 # spacing3=3, wrap="word", )
def build_pane(self, weight): self.frame = ttk.Frame(self.parent) self.parent.add(self.frame, weight=weight) self.menu_frame = ttk.Frame(self.frame) #, background=bg_color()) self.menu_frame.pack(side='top', fill='x') self.add_module_button = tk.Label( self.menu_frame, image=icons.get_icon("plus-lightgray"), bg=bg_color(), cursor='hand2') self.add_module_button.bind('<Button-1>', self.add_empty_module_window) #self.add_module_button = tk.Button(self.menu_frame, text='Add Module', fg=text_color(), bg=bg_color(), cursor='hand2') self.add_module_button.pack(side='left', padx=20) #self.add_module_button.bind('<Button-1>', self.add_module_window) self.close_icon = icons.get_icon('x-lightgray') self.x_button = tk.Label(self.menu_frame, text='-', fg=text_color(), bg=bg_color(), cursor='hand2') self.x_button.pack(side='right', padx=20) self.x_button.bind( '<Button-1>', lambda event, pane=self: self.hide_pane_callback(pane=self) ) #lambda event, pane_name=self.name: destroy_callback(pane_name=pane_name)) self.pane = ttk.PanedWindow(self.frame, orient=self.orient) self.pane.pack(side='top', fill='both', expand=True) self.hidden = False
def build(self, options, selection_callback, destroy_callback): self.frame = ttk.Frame( self.parent.pane, borderwidth=2, relief='sunken') #, height=1, background=bg_color()) self.parent.pane.add(self.frame, weight=1) self.menu_frame = ttk.Frame(self.frame) self.menu_frame.pack(side='top', fill='x', expand=False) # make dropdown for selecting a module self.module_selection.set('None') self.module_menu = tk.OptionMenu(self.menu_frame, self.module_selection, *options) self.module_menu.pack(side='left', expand=True, padx=20) self.module_selection.trace( 'w', lambda a, b, c, module_window=self: selection_callback( module_window=module_window)) self.close_icon = icons.get_icon('x-lightgray') self.x_button = tk.Label(self.menu_frame, text='тип', fg=text_color(), bg=bg_color(), cursor='hand2') self.x_button.pack(side='right', padx=20) self.x_button.bind('<Button-1>', lambda event, module_window=self: destroy_callback( module_window=module_window))
def update_search_results(self, num_matches, active_index=1): if not self.search_results: self.search_results = tk.Label(self.search_frame, bg=bg_color(), fg=text_color()) self.search_results.pack(side='left', padx=5) if num_matches == 0: self.search_results.config(text='No matches') else: self.search_results.config(text=f'{active_index}/{num_matches}')
def open_search(self): self.close_search() self.search_frame.pack(side='bottom', expand=False, fill='x') self.search_label = tk.Label(self.search_frame, text='Search:', bg=bg_color(), fg=text_color()) self.search_label.pack(side='left', expand=True) self.search_box = TextAware(self.search_frame, bd=2, height=1) self.search_box.pack(side='left', expand=True, fill='x', padx=5) self.search_box.configure( font=self.font, foreground=text_color(), background=bg_color(), ) if not self.case_sensitive: self.case_sensitive = tk.BooleanVar(value=0) self.case_sensitive_checkbox = ttk.Checkbutton( self.search_frame, text='Aa', variable=self.case_sensitive, ) self.case_sensitive_checkbox.pack(side='left', expand=True, padx=5) #self.search_close_button = ttk.Button(self.search_frame, text='[x]', command=self.exit_search, width=2.5) self.search_close_button = tk.Label(self.search_frame, text='тип', font=("Arial", 12), fg=text_color(), bg=bg_color(), cursor='hand2') self.search_close_button.bind('<Button-1>', self.exit_search) self.search_close_button.pack(side='left', expand=True, padx=2) self.search_box.focus() self.search_box.bind("<Key>", lambda event: self.search_key_pressed(event))
def textbox_config(fg=text_color(), bg=bg_color(), font='Georgia', size=12, spacing1=10, spacing2=8, pady=5): return { 'font': Font(family=font, size=size), 'spacing1': spacing1, # spacing between paragraphs 'foreground': fg, 'background': bg, 'padx': 2, 'pady': pady, 'spacing2': spacing2, # Spacing between lines 'spacing3': 5, 'wrap': "word", 'insertbackground': fg }
def refresh(self): if self.new_button: self.new_button.destroy() for memory in self.memories: memory.destroy() for check in self.checks: check.destroy() for edit_button in self.edit_buttons: edit_button.destroy() self.memories = [] self.checks = [] self.edit_buttons = [] for i, memory in enumerate(self.state.construct_memory(self.node)): if memory['text']: temp_check = tk.BooleanVar() temp_check.set(True) row = self.master.grid_size()[1] self.memories.append(TextAware(self.master, height=1)) self.memories[i].grid(row=row, column=0, columnspan=2, padx=5) self.memories[i].insert(tk.INSERT, memory['text']) self.memories[i].configure( state='disabled', foreground=text_color(), background=bg_color(), wrap="word", ) # FIXME checks are unchecked by default self.checks.append( tk.Checkbutton(self.master, variable=temp_check)) self.checks[i].grid(row=row, column=2, padx=3) self.edit_buttons.append( create_button( self.master, "Edit", lambda _memory=memory: self.edit_memory(_memory), width=4)) self.edit_buttons[i].grid(row=row, column=3) self.new_button = create_button(self.master, "Add memory", self.create_new, width=11)
def refresh(self): if self.new_button: self.new_button.destroy() for memory in self.memories: memory.destroy() for edit_button in self.edit_buttons: edit_button.destroy() self.memories = [] self.checks = [] self.edit_buttons = [] if 'memories' in self.node: for i, memory_id in enumerate(self.node['memories']): memory = self.state.memories[memory_id] if memory['text']: row = self.master.grid_size()[1] self.memories.append(TextAware(self.master, height=1)) self.memories[i].grid(row=row, column=0, columnspan=2, padx=5) self.memories[i].insert(tk.INSERT, memory['text']) self.memories[i].configure( state='disabled', foreground=text_color(), background=bg_color(), wrap="word", ) self.edit_buttons.append( create_button( self.master, "Edit", lambda _memory=memory: self.edit_memory(_memory), width=4)) self.edit_buttons[i].grid(row=row, column=3) self.new_button = create_button(self.master, "Add memory", self.create_new, width=11)
def _build_textbox(self, frame, frame_attr, textbox_attr, height=1): textbox_frame = ttk.Frame(frame) self.__setattr__(frame_attr, textbox_frame) scrollbar = ttk.Scrollbar(textbox_frame, command=lambda *args: self.__getattribute__( textbox_attr).yview(*args)) scrollbar.pack(side=tk.RIGHT, fill=tk.Y) textbox = TextAware(textbox_frame, bd=3, height=height, yscrollcommand=scrollbar.set, undo=True) self.__setattr__(textbox_attr, textbox) # TODO move this out textbox.bind("<Control-Button-1>", lambda event: self.edit_history(txt=textbox)) textbox.bind("<Control-Shift-Button-1>", lambda event: self.goto_history(txt=textbox)) textbox.bind("<Control-Alt-Button-1>", lambda event: self.split_node(txt=textbox)) textbox.bind("<Alt-Button-1>", lambda event: self.select_token(txt=textbox)) textbox.pack(expand=True, fill='both') readable_font = Font( family="Georgia", size=12) # Other nice options: Helvetica, Arial, Georgia textbox.configure( font=readable_font, spacing1=10, foreground=text_color(), background=bg_color(), padx=2, pady=5, spacing2=8, # Spacing between lines4 spacing3=5, wrap="word", )
def body(self, master): create_side_label(master, "id") create_label(master, self.node["id"], row=master.grid_size()[1] - 1, col=1, padx=15) create_side_label(master, "bookmarked") create_label(master, "true" if self.node.get("bookmark", False) else "false", row=master.grid_size()[1] - 1, col=1, padx=15) create_side_label(master, "visited") create_label(master, "true" if self.node.get("visited", False) else "false", row=master.grid_size()[1] - 1, col=1, padx=15) create_side_label(master, "canonical") create_label(master, "true" if self.node["id"] in self.state.calc_canonical_set() else "false", row=master.grid_size()[1] - 1, col=1, padx=15) if "meta" in self.node: meta = self.node["meta"] create_side_label(master, "origin") if "origin" in meta: create_label(master, meta["origin"], row=master.grid_size()[1] - 1, col=1, padx=15) else: create_label(master, "unknown", row=master.grid_size()[1] - 1, col=1, padx=15) if "generation" in meta: create_side_label(master, "prompt") prompt_text = tk.Text(master, height=15) prompt_text.grid(row=master.grid_size()[1] - 1, column=1) prompt_text.insert(tk.INSERT, meta["generation"]["prompt"]) prompt_text.configure( state='disabled', spacing1=8, foreground=text_color(), background=bg_color(), wrap="word", ) # makes text copyable prompt_text.bind("<Button>", lambda event: prompt_text.focus_set()) create_side_label(master, "original generated text") gen_text = tk.Text(master, height=5) gen_text.grid(row=master.grid_size()[1] - 1, column=1) gen_text.insert(tk.INSERT, meta["generation"]["text"]) gen_text.tag_config("prob_1", background=PROB_1) gen_text.tag_config("prob_2", background=PROB_2) gen_text.tag_config("prob_3", background=PROB_3) gen_text.tag_config("prob_4", background=PROB_4) gen_text.tag_config("prob_5", background=PROB_5) gen_text.tag_config("prob_6", background=PROB_6) # TODO continuous coloration for i, position in enumerate( meta["generation"]["logprobs"]["text_offset"]): prob = math.exp( meta["generation"]["logprobs"]["token_logprobs"][i]) #index_offset = position - len(meta["generation"]["prompt"]) token_length = len( meta["generation"]["logprobs"]["tokens"][i]) if prob >= 0.8: gen_text.tag_add( "prob_1", f"1.0 + {position} chars", f"1.0 + {position + token_length} chars") elif prob >= 0.6: gen_text.tag_add( "prob_2", f"1.0 + {position} chars", f"1.0 + {position + token_length} chars") elif prob >= 0.4: gen_text.tag_add( "prob_3", f"1.0 + {position} chars", f"1.0 + {position + token_length} chars") elif prob >= 0.2: gen_text.tag_add( "prob_4", f"1.0 + {position} chars", f"1.0 + {position + token_length} chars") elif prob >= 0.05: gen_text.tag_add( "prob_5", f"1.0 + {position} chars", f"1.0 + {position + token_length} chars") else: gen_text.tag_add( "prob_6", f"1.0 + {position} chars", f"1.0 + {position + token_length} chars") gen_text.configure(state='disabled') # gen_text.configure( # state='disabled', # font=readable_font, # spacing1=10, # foreground=text_color(), # background=bg_color(), # wrap="word", # ) # makes text copyable gen_text.bind("<Button>", lambda event: gen_text.focus_set()) create_side_label(master, "model") create_label(master, meta["generation"]["model"], row=master.grid_size()[1] - 1, col=1, padx=15)
def search_results(self, matches, start=0): # remove previous search results context_padding = 50 limit = 4 counter = 0 if self.num_results_label: self.num_results_label.destroy() if self.next_page_button: self.next_page_button.destroy() if self.prev_page_button: self.prev_page_button.destroy() for result in self.results: result.destroy() for label in self.labels: label.destroy() for button in self.goto_buttons: button.destroy() self.results = [] self.labels = [] self.goto_buttons = [] self.num_results_label = create_side_label(self.master, f'{len(matches)} results') for i, match in enumerate(matches[start:]): if counter >= limit: break node = self.state.tree_node_dict[match['node_id']] self.labels.append( create_side_label( self.master, f"chapter: {self.state.chapter(node)['title']}")) #side_label.config(fg="blue") self.results.append(TextAware(self.master, height=2)) #readable_font = Font(family="Georgia", size=12) self.results[i].configure( #font=readable_font, spacing1=8, foreground=text_color(), background=bg_color(), wrap="word", ) self.results[i].grid(row=self.master.grid_size()[1] - 1, column=1) node_text = node["text"] start_index = max(0, match['span'][0] - context_padding) end_index = min(len(node_text), match['span'][1] + context_padding) text_window = node_text[start_index:end_index] self.results[i].insert(tk.INSERT, text_window) self.results[i].tag_configure("blue", background="blue") self.results[i].highlight_pattern(match['match'], "blue") self.results[i].configure(state='disabled') # makes text copyable # binding causes computer to freeze #self.results[i].bind("<Button>", lambda event, _result=self.results[i]: result.focus_set()) #matched_text.bind("<Alt-Button-1>", lambda event: self.goto_result(match['node_id'])) self.goto_buttons.append( create_button( self.master, "go to match", lambda _match=match: self.goto_result(_match['node_id']))) self.goto_buttons[i].grid(row=self.master.grid_size()[1] - 2, column=2) counter += 1 if start > 0: self.prev_page_button = create_button( self.master, "previous page", lambda _start=start, _limit=limit: self.search_results( matches, start=_start - _limit)) self.prev_page_button.config(width=12) if len(matches) > start + limit: self.next_page_button = create_button( self.master, "next page", lambda _start=start, _limit=limit: self.search_results( matches, start=_start + _limit))
def build_side(self, frame): self.side_frame = ttk.Frame(frame, height=500, width=300, relief='sunken', borderwidth=2) self.notes_options_frame = ttk.Frame(self.side_frame, height=200, width=300) self.notes_options_frame.pack(fill='both') tk.Label(self.notes_options_frame, text="Select note", bg=bg_color(), fg=text_color()).grid(column=0, row=0) placeholder_options = ('aaaaa', 'bbbbbb', 'cccccc') v = tk.StringVar() v.set(placeholder_options[0]) self.notes_select = tk.OptionMenu(self.notes_options_frame, v, *placeholder_options) #self.notes_select['menu'].config(relief='raised') self.notes_select.grid(column=1, row=0) tk.Label(self.notes_options_frame, text="Scope", bg=bg_color(), fg=text_color()).grid(column=2, row=0, padx=5) scope_options = ('node', 'subtree', 'global') v = tk.StringVar() v.set(scope_options[0]) self.scope_select = tk.OptionMenu(self.notes_options_frame, v, *scope_options) self.scope_select.grid(column=3, row=0) tk.Label(self.notes_options_frame, text="Note title", bg=bg_color(), fg=text_color()).grid(column=0, row=1) self.notes_title = tk.Entry(self.notes_options_frame, bg=bg_color(), fg=text_color(), relief='sunken') self.notes_title.grid(column=1, row=1, padx=10) tk.Label(self.notes_options_frame, text="Root node", bg=bg_color(), fg=text_color()).grid(column=2, row=1) tk.Label(self.notes_options_frame, text="placeholder id ...", bg=bg_color(), fg='blue').grid(column=3, row=1) self.change_root_button = tk.Button(self.notes_options_frame, text='Change', bg=bg_color(), fg=text_color()) self.change_root_button.grid(column=4, row=1) self.change_root_button = tk.Button(self.notes_options_frame, text='Delete', bg=bg_color(), fg=text_color()) self.change_root_button.grid(column=5, row=1, padx=10) self.notes_frame = ttk.Frame(self.side_frame) self.notes_frame.pack(expand=True, fill='both') self._build_textbox(self.notes_frame, "notes_textbox_frame", "notes_textbox", height=1) self.notes_textbox_frame.pack(expand=True, fill="both")