coretk - changes to have app load all images, and some small cleanup
This commit is contained in:
parent
6f916995d7
commit
d88ea50ad2
3 changed files with 52 additions and 34 deletions
|
@ -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
|
||||
|
|
|
@ -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, "<ButtonPress-1>", self.click_press)
|
||||
self.canvas.tag_bind(self.id, "<ButtonRelease-1>", self.click_release)
|
||||
|
|
15
coretk/coretk/icons.py
Normal file
15
coretk/coretk/icons.py
Normal file
|
@ -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]
|
Loading…
Add table
Reference in a new issue