Aareon / Aareon/Codingg

Update TextArea to inherit from tk.Canvas

オープン
#3 コメント 0 件 リアクション 0 件 担当者 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 を短くまとめたダイジェスト。