Update TextArea to inherit from tk.Canvas
- 主要语言
- 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 还没有评估数据。