def search(self, text: tk.Text, tag: str, master: tk.Frame) -> None: """ Search the document for a certain string and mark it :param text: The text field to search :param tag: The tag to highlight the text with :param master: The main window :type text: tk.Text :type tag: str :type master: tk.Frame """ dialog = search_dialog.SearchDialog(master) master.wait_window(dialog.top) keyword = master.received_data pos = '1.0' while True: countVar = tk.StringVar() idx = text.search(keyword, pos, tk.END, regexp=master.search_with_regex, count=countVar, nocase=master.ignore_case) if not idx: break pos = f'{idx}+{countVar.get()}c' text.tag_add(tag, idx, pos)
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_textbox(self, tbox: tk.Text, words, find_indexces=None): # tBoxをにwordsで上書き tbox.delete('1.0', 'end') [tbox.insert(tk.INSERT, word) for word in words] if find_indexces: for i in find_indexces: tbox.tag_add("highlight", '{}.0'.format(i + 1), "{}.0+1lines".format(i + 1))
def insert_change_of_line(differences: tuple, index: int, widget: tkinter.Text, num_line: int): ''' Add one line which is changed. The reason for this function is that, when a line is totally changed, it is considered as removed; when small part is changed, the next element of differences indicates the different chars. Arguments: differences: list of differences computed with difflib. index: the index of the difference to check. widget: where to write. num_line: number of line of the index line, for font and style. Returns: A list of start-end index. ''' if index + 1 < len(differences) and differences[index + 1][0] == '?': widget.insert(tkinter.END, differences[index][2:] + '\n') for r in parse_line_index(differences[index + 1][2:]): widget.tag_add('diff', f'{num_line}.{r[0]}', f'{num_line}.{r[1]}') else: # this line should be partially changed only, mark with less distinction for safety widget.insert(tkinter.END, differences[index][2:] + '\n', 'should-partial')
def __init__( self, textwidget: tkinter.Text, start_lineno: int, middle_lineno: int, end_lineno: int ) -> None: self.textwidget = textwidget n = next(tag_counter) self.part1_tag = f"merge_conflict_{n}_part1" self.middle_tag = f"merge_conflict_{n}_middle" self.part2_tag = f"merge_conflict_{n}_part2" part1_color = utils.mix_colors(self.textwidget["bg"], "magenta", 0.8) manual_color = utils.mix_colors(self.textwidget["bg"], "tomato", 0.8) part2_color = utils.mix_colors(self.textwidget["bg"], "cyan", 0.8) # TODO: also specify fg color self.part1_button = self.make_button( start_lineno, part1_color, text="Use this", command=self.use_part1 ) self.manual_button = self.make_button( middle_lineno, manual_color, text="Edit manually", command=self.stop_displaying ) self.part2_button = self.make_button( end_lineno, part2_color, text="Use this", command=self.use_part2 ) textwidget.tag_config(self.part1_tag, background=part1_color) textwidget.tag_config(self.middle_tag, background=manual_color) textwidget.tag_config(self.part2_tag, background=part2_color) textwidget.tag_lower(self.part1_tag, "sel") textwidget.tag_lower(self.middle_tag, "sel") textwidget.tag_lower(self.part2_tag, "sel") textwidget.tag_add(self.part1_tag, f"{start_lineno}.0", f"{middle_lineno}.0") textwidget.tag_add(self.middle_tag, f"{middle_lineno}.0", f"{middle_lineno + 1}.0") textwidget.tag_add(self.part2_tag, f"{middle_lineno + 1}.0", f"{end_lineno + 1}.0") self._stopped = False
def __format_text(text_widget: tk.Text, text: str): extracted_text, tags_dict = parse_text(text) text_widget.insert(1.0, extracted_text) for key, list_ in tags_dict.items(): for start, len_ in list_: text_widget.tag_add(key, f'1.0+{start}c', f'1.0+{start+len_}c')