Home

Tuesday, February 22, 2022

Python 3 - Win10: Tkinter - Word Processor - Text Widget - Font Styling 2

#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
 

 

-END-



Sunday, February 13, 2022

Update! 'How To' Video added - RPG Systems Simple Validate User Input System 1

I released a 'How to' video for the 'RPG Systems - simple input validation' post.

find the video here:

the post here:

Please note that the post was released a while before the video so the code has changed a little.


Monday, February 7, 2022

Python 3 - Win10: Tkinter - Word Processor - Text Widget - Line Wrapping - Find Wrap Position 2

#Python 3 - Win10

Tkinter Word Processor

Text Widget: Line Wrapping

Find Wrap Position 2

Brief:

_______________________________________________________________
So as it turns out I didn't need to do any of that heavy lifting in my previous post. There were methods in place to do what I wanted. I found the answer in the tkinter/tcl docs while trying to figure out a different problem with the word processor application. I've been using this functions all the time but did not realize I could use the "display" sub-modifier when calling indices. But it's live and learn with coding.
 
The code is so minuscule it makes me want to cry for the time I took brute forcing the functionality.
 

 

Functionality:

_______________________________________________________________
...

Improvements:
  • N/A
Issues:
  •  N/A

 

Demo:

_______________________________________________________________
 N/A

 

Code Snippet: 

_______________________________________________________________

import tkinter as tk


def inspect_wrapline(event=None):
  start = f"{tk.INSERT} linestart"
  end   = f"{tk.INSERT} lineend"
  counter = event.widget.count(start, end, "displaylines")
  
  print("number of lines after first line", counter)


  start = f"{tk.INSERT} display linestart"
  end   = f"{tk.INSERT} display lineend +1c"
  dline = event.widget.get(start, end)

  print("Text on the displayline", dline)


root_window = tk.Tk()
text_widget = tk.Text(root_window)
text_widget.pack()
text_widget.insert("This is an example text.")
text_widget.bind("<Control-l>", inspect_wrapline)

root_window.mainloop()
 

Credits and Resources:

  • Code formatted via: http://hilite.me/ : Styling: Python, monokai
  1. https://tcl.tk/man/tcl8.6/TkCmd/text.htm
  2. https://tkdocs.com/tmp-pyref/onepage.html
 
  • https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/text-methods.html
  • https://docs.python.org/3/library/tkinter.ttk.html
  • https://stackoverflow.com/

 

-END-