#Python 3+ Win 10
Tkinter Word Processor
Text Widget Font Styling 2
Brief:
_______________________________________________________________
In the previous post
here
I could not figure out how to avoid the styling artifacts of color not
updating quickly enough or the tag range being cut off while typing quickly.
I solved this issue with the code below.
in the original post I can remove carry_over, valid_over, in any_key_down replace the code with that of the code
snippet.
Demo:
_______________________________________________________________
How To:
Code Snippet:
_______________________________________________________________
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import tkinter as tk import re class Document(tk.Text): def __init__(self, master, *args, **kewargs): super().__init__(*args, **kewargs) self.master = master self.master.bind("<KeyPress>", self.any_key_down) self.tag_add('<b>', "1.0", "1.2") self.tag_config('<b>', foreground='red') def any_key_down(self, event=None): print(event) char = re.findall(r"[a-zA-Z0-9\S ]", event.char) if 0 < len(char) and event.keysym not in ["BackSpace", "Escape"] or '\t' == event.char: insert = event.widget.index('insert-2c') tags = event.widget.tag_names(insert) for tag in tags: event.widget.tag_add(tag, 'insert-1c', 'insert') root = tk.Tk() text = Document(root, height=4) text.pack() text.insert("1.0", "This is helvetica font", "<b>") text.insert("1.0", "This is terminal font", "font_term") text.tag_config('<b>', font='Helvetica 12') text.tag_config('font_term', font='Terminal 12') text.insert("3.0", "This is terminal font\n", "font_term") root.mainloop() |
Credits & Resources:
- Code formatted via: http://hilite.me/ : Styling: Python, monokai