Aareon / Aareon/Codingg

Update TextArea to inherit from tk.Canvas

Offen
#3 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
Python
Sterne
6
Forks
0
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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.

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.