Aareon / Aareon/Codingg

Update TextArea to inherit from tk.Canvas

Open
#3 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
6
Forks
0
PR merge metrics
No merged PRs in 30d

Description

I'd like to use a `tk.Canvas` rather than a `tk.TextEdit` so that we can leverage `PIL` in order to use fonts not installed on the host system. Below is a rough example of how this would work.

```python3
import tkinter as tk
from typing import Union

from PIL import Image, ImageDraw, ImageFont, ImageTk

class Example(tk.Frame):
def __init__(self):
super().__init__(self)

self.canvas = tk.Canvas(self, width=100, height=100)
self.canvas.pack()

self.text_area_font_size = 14
self.text_area_font = ImageFont.truetype(
"some/file/path.ttf",
self.text_area_font_size
)

# create an image where all of our text area activity will occur
self.text_area = Image.new("RGBA", (100, 100))

def draw_text(
self,
text: str,
pos: Union[list, tuple],
font: ImageFont = None,
fill: Union[list,tuple] = (255,255,255,128)
):
if font is None:
font = self.font

# create a drawing context and draw the text
d = ImageDraw.Draw(self.text_area)
d.text(pos, text, font=font, fill=fill)

# convert the image to something Tkinter can use
image = ImageTk.PhotoImage(self.text_area)
self.canvas.create_image(0, 0, anchor=tk.NW, image=image)

# update the window to display the change
self.update()
```

This will also give us the ability to draw things in the text area besides just text. We'd be able to draw things such as indent guides, linting indicators, as well as boxes for displaying various bits of information. Think "peek references" and function inspection in other established editors.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.