From d88ea50ad232a5f8ab2ed11ddc9801abaf1864c6 Mon Sep 17 00:00:00 2001 From: Blake Harnden Date: Sun, 15 Sep 2019 23:45:13 -0700 Subject: [PATCH] coretk - changes to have app load all images, and some small cleanup --- coretk/coretk/app.py | 49 ++++++++++++++++++++++++++---------------- coretk/coretk/graph.py | 22 ++++++------------- coretk/coretk/icons.py | 15 +++++++++++++ 3 files changed, 52 insertions(+), 34 deletions(-) create mode 100644 coretk/coretk/icons.py diff --git a/coretk/coretk/app.py b/coretk/coretk/app.py index e8b559a5..9294b523 100644 --- a/coretk/coretk/app.py +++ b/coretk/coretk/app.py @@ -1,26 +1,29 @@ import tkinter as tk -from PIL import Image, ImageTk from coretk.graph import CanvasGraph +from coretk.icons import Images class Application(tk.Frame): def __init__(self, master=None): super().__init__(master) - self.master.title("CORE") - self.master.geometry("800x600") - self.master.state("zoomed") - self.set_icon() - self.pack(fill=tk.BOTH, expand=1) - self.images = [] + self.load_images() + self.setup_app() self.menubar = None self.create_menu() self.create_widgets() - def set_icon(self): - image = Image.open("core-icon.png") - tk_image = ImageTk.PhotoImage(image) - self.master.tk.call("wm", "iconphoto", self.master._w, tk_image) + def load_images(self): + Images.load("switch", "switch.png") + Images.load("core", "core-icon.png") + + def setup_app(self): + self.master.title("CORE") + self.master.geometry("800x600") + self.master.state("zoomed") + image = Images.get("core") + self.master.tk.call("wm", "iconphoto", self.master._w, image) + self.pack(fill=tk.BOTH, expand=True) def create_menu(self): self.master.option_add("*tearOff", tk.FALSE) @@ -34,26 +37,34 @@ class Application(tk.Frame): self.master.config(menu=self.menubar) def create_widgets(self): + image = Images.get("switch") edit_frame = tk.Frame(self) edit_frame.pack(side=tk.LEFT, fill=tk.Y, ipadx=2, ipady=2) radio_value = tk.IntVar() - b = tk.Radiobutton(edit_frame, text="Button 1", indicatoron=False, variable=radio_value, value=1) + b = tk.Radiobutton( + edit_frame, indicatoron=False, variable=radio_value, value=1, image=image + ) b.pack(side=tk.TOP, pady=1) - b = tk.Radiobutton(edit_frame, text="Button 2", indicatoron=False, variable=radio_value, value=2) + b = tk.Radiobutton( + edit_frame, indicatoron=False, variable=radio_value, value=2, image=image + ) b.pack(side=tk.TOP, pady=1) - b = tk.Radiobutton(edit_frame, text="Button 3", indicatoron=False, variable=radio_value, value=3) + b = tk.Radiobutton( + edit_frame, indicatoron=False, variable=radio_value, value=3, image=image + ) b.pack(side=tk.TOP, pady=1) - b = tk.Radiobutton(edit_frame, text="Button 4", indicatoron=False, variable=radio_value, value=4) + b = tk.Radiobutton( + edit_frame, indicatoron=False, variable=radio_value, value=4, image=image + ) b.pack(side=tk.TOP, pady=1) - b = tk.Radiobutton(edit_frame, text="Button 5", indicatoron=False, variable=radio_value, value=5) + b = tk.Radiobutton( + edit_frame, indicatoron=False, variable=radio_value, value=5, image=image + ) b.pack(side=tk.TOP, pady=1) self.canvas = CanvasGraph( self, background="#cccccc", scrollregion=(0, 0, 1000, 1000) ) - self.canvas.load("switch", "switch.png") - self.canvas.add_node(50, 50, "Node 1", "switch") - self.canvas.add_node(50, 100, "Node 2", "switch") self.canvas.pack(fill=tk.BOTH, expand=True) scroll_x = tk.Scrollbar( self.canvas, orient=tk.HORIZONTAL, command=self.canvas.xview diff --git a/coretk/coretk/graph.py b/coretk/coretk/graph.py index d6e73e3c..4e5cef74 100644 --- a/coretk/coretk/graph.py +++ b/coretk/coretk/graph.py @@ -1,7 +1,7 @@ import enum import tkinter as tk -from PIL import Image, ImageTk +from coretk.icons import Images class GraphMode(enum.Enum): @@ -11,14 +11,6 @@ class GraphMode(enum.Enum): class CanvasGraph(tk.Canvas): - images = {} - - @classmethod - def load(cls, name, file_path): - image = Image.open(file_path) - tk_image = ImageTk.PhotoImage(image) - cls.images[name] = tk_image - def __init__(self, master=None, cnf=None, **kwargs): if cnf is None: cnf = {} @@ -78,7 +70,7 @@ class CanvasGraph(tk.Canvas): self.handle_edge_release(event) elif self.mode == GraphMode.NODE: x, y = self.canvas_xy(event) - self.add_node(x, y, "Node", "switch") + self.add_node(x, y, "switch") def handle_edge_release(self, event): edge = self.drawing_edge @@ -146,9 +138,9 @@ class CanvasGraph(tk.Canvas): self.mode = GraphMode.NODE print(f"graph mode: {self.mode}") - def add_node(self, x, y, name, image_name): - image = self.images[image_name] - node = CanvasNode(x, y, name, image, self) + def add_node(self, x, y, image_name): + image = Images.get(image_name) + node = CanvasNode(x, y, image, self) self.nodes[node.id] = node return node @@ -175,13 +167,13 @@ class CanvasEdge: class CanvasNode: - def __init__(self, x, y, name, image, canvas): - self.name = name + def __init__(self, x, y, image, canvas): self.image = image self.canvas = canvas self.id = self.canvas.create_image( x, y, anchor=tk.CENTER, image=self.image, tags="node" ) + self.name = f"Node {self.id}" self.text_id = self.canvas.create_text(x, y + 20, text=self.name) self.canvas.tag_bind(self.id, "", self.click_press) self.canvas.tag_bind(self.id, "", self.click_release) diff --git a/coretk/coretk/icons.py b/coretk/coretk/icons.py new file mode 100644 index 00000000..94517a14 --- /dev/null +++ b/coretk/coretk/icons.py @@ -0,0 +1,15 @@ +from PIL import Image, ImageTk + + +class Images: + images = {} + + @classmethod + def load(cls, name, file_path): + image = Image.open(file_path) + tk_image = ImageTk.PhotoImage(image) + cls.images[name] = tk_image + + @classmethod + def get(cls, name): + return cls.images[name]