Last active
February 25, 2018 17:51
-
-
Save acbart/63f33e11116d58d9636222b11b85f00f to your computer and use it in GitHub Desktop.
Some proposed modifications to tktextext.py in order to support copy/paste of images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example URL: https://i.imgur.com/TqN0Be5.png | |
# This let's you copy/paste a URL into your source code (png, bmp anyway) | |
import base64 | |
# This is for copying HTTP images | |
from urllib.request import urlopen | |
... | |
class EnhancedText(TweakableText): | |
def __init__(self, master=None, cnf={}, **kw): | |
... | |
# Need to store refs to the images, or they disappear immediately | |
# Maps unique string names to the PhotoImage instances | |
self._images = [] | |
self._IMAGE_COUNTER = 0 | |
... | |
def _bind_editing_aids(self): | |
... | |
# Bind paste functionality | |
self.bind("<<Paste>>", if_not_readonly(self.paste_image), True) | |
... | |
... | |
def paste_image(self, event): | |
''' | |
Embeds images in the text | |
''' | |
# Retrieve string URL from clipboard | |
image_url = self.clipboard_get() | |
# HTTP GET the contents of the URL | |
try: | |
image_data = urlopen(image_url).read() | |
except ValueError: | |
return | |
# Convert to a base64 representation - might want to cache to file | |
# instead, but this is a simple version. | |
image_b64 = base64.encodestring(image_data) | |
# Load base64 rep into a Tkinter PhotoImage | |
try: | |
image_tk = tk.PhotoImage(data = image_b64) | |
except ValueError: | |
return | |
# Make a unique name for this URL so we can find it later | |
unique_name = str(self._IMAGE_COUNTER)+'|'+image_url | |
self._IMAGE_COUNTER += 1 | |
# Keep track of the data internally for future lookups | |
self._images.append((image_tk, unique_name, image_url)) | |
# Actually create the image within the Text editor | |
self.image_create("insert", image=image_tk, name=unique_name) | |
# After the event is done, we restore the URL to the clipboard in | |
# case the user wants to use it again. | |
self.after(1, lambda: self.clipboard_append(image_url)) | |
# Clear the clipboard to prevent the URL from appearing. | |
self.clipboard_clear() | |
def get_images(self): | |
''' | |
Processes a list of the images embedded in the text, their URLs as | |
strings, and their positions within the text. | |
''' | |
for image_tk, unique_name, image_url in self._images: | |
# TODO: do some sanity check to make sure the name exists, since | |
# it could have been deleted | |
line_column_position = self.index(unique_name) | |
# With the line/column position and the image_url, you could work | |
# backwards through the Text editor's contents to inject the URLS | |
# into their right places. This would need to be done whenever | |
# you want to get access to the contents of the editor. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment