Last active
February 25, 2019 16:46
-
-
Save davidkwast/dc01aee49153c75b0f1b16a44bd8857b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import tkinter as tk | |
import tkinter.filedialog as filedialog | |
# from pillow import Image, ImageTk | |
class ImageLayer: | |
def __init__(self): | |
self.width = 0 | |
self.height = 0 | |
self.x = 0 | |
self.y = 0 | |
self.path = "" | |
self.img = None | |
class LayeredImageBuilderGUI(tk.Frame): # HERE | |
def __init__(self, master): | |
super(LayeredImageBuilderGUI, self).__init__(master) | |
self.pack() | |
self.master = master | |
self.canvas = tk.Canvas(self, width=1024, height=768) | |
self.canvas.pack(expand=True, fill=tk.BOTH) | |
self.master.bind('<Button-3>', self.display_contextual) | |
self.init_contextual() | |
self.images = [] | |
self.photoimg_objs = [] | |
# self.loop() # HERE | |
def init_contextual(self): | |
self.contextual_menu = tk.Menu(self, tearoff=0) | |
self.contextual_menu.add_command(label='Add Image', | |
command=self.add_image) | |
def display_contextual(self, event): | |
try: | |
self.contextual_menu.tk_popup(event.x_root, event.y_root, 0) | |
except Exception: | |
pass | |
finally: | |
# make sure to release the grab (Tk 8.0a1 only) | |
self.contextual_menu.grab_release() | |
def add_image(self): | |
img_path = filedialog.askopenfilename() | |
im = tk.PhotoImage(file=img_path) | |
self.photoimg_objs.append(im) # HERE | |
layer = ImageLayer() | |
layer.width = im.width() | |
layer.height = im.height() | |
layer.path = img_path | |
layer.img = self.canvas.create_image(0, 0, image=im, anchor="nw") | |
print(layer.img) | |
self.images.append(layer) | |
# return | |
# def loop(self): | |
# self.canvas.update_idletasks() | |
# self.after(5, self.loop) | |
if __name__ == '__main__': | |
root = tk.Tk() # HERE | |
LayeredImageBuilderGUI(master=root) # HERE | |
root.mainloop() # HERE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment