Aareon / Aareon/Codingg

Update TextArea to inherit from tk.Canvas

未關閉
#3 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
Python
星號
6
分支
0
PR 合併指標
30 天內沒有已合併 PR

描述

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.

貢獻指南

這個儲存庫沒有索引到貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。