Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.9 kB
1
Indexable
Never
# Imports

import sys
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
import json


# Interface

root = tk.Tk()
root.title("Grid")
root.iconbitmap(bitmap = "paint.ico")


# Subroutines

def boxEnter(event):
    obj = event.widget
    obj["relief"] = "sunken"



def boxLeave(event):
    obj = event.widget
    obj["relief"] = "groove"



def boxClick(event):
    obj = event.widget
    obj["bg"] = colors[color_index]



def colorChange(event):
    global color_index
    color_index += 1
    if color_index == len(colors): color_index = 0
    labelPenColor["bg"] = colors[color_index]

def boxErase(event):
    obj = event.widget
    obj["bg"] = bg_default

def gridSave():
    fp = open("gridData.txt", "w")
    gridData = []
    for y in range(rows):
        row = []
        for x in range(columns):
            row.append(box[y][x]["bg"])
        gridData.append(row)
    json.dump(gridData, fp)


def gridLoad():
    fp = open("gridData.txt", "r")
    gridData = json.load(fp)
    for y in range(rows):
        for x in range(columns):
            box[y][x]["bg"] = gridData[y][x]

def gridErase():
    fp = open("gridData", "w")
    for y in range(rows):
        for x in range(columns):
            box[y][x]["bg"] = "systemButtonFace"


# Frame

frameGrid = tk.Frame(root)
frameGrid.grid(column = 0, row = 0)
frameInfo = tk.Frame(root)
frameInfo.grid(column = 1, row = 0)


# Grid Editing

rows = 20
columns = 20
box = []
for i in range(rows):
    box.append([None] * columns)
colors = ["Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet", "White", "Black"]
color_index = 0


# Pen editing

## Pen Title Editing
labelPenTitle= tk.Label(frameInfo)
labelPenTitle["text"] = "Pen Color"
labelPenTitle.grid(column = 0, row = 0)

## Pen Colour Editing
labelPenColor = tk.Label(frameInfo)
labelPenColor.grid(column = 0, row = 1)
labelPenColor["width"] = 4
labelPenColor["height"] = 2
labelPenColor["relief"] = "groove"

labelPenColor["bg"] = colors[0]

# Box Generation

for y in range(rows):
    for x in range(columns):
        box[y][x] = tk.Label(frameGrid)
        box[y][x].grid(column = x, row = y)
        box[y][x]["width"] = 4
        box[y][x]["height"] = 2
        box[y][x]["relief"] = "groove"
        box[y][x].bind("<Enter>", boxEnter)
        box[y][x].bind("<Leave>", boxLeave)
        box[y][x].bind("<Button-1>", boxClick)
        box[y][x].bind("<Button-3>", boxErase)
        labelPenColor.bind("<Button-1>", colorChange)
bg_default = box[0][0]["bg"]

# Saving feature

ttk.Button(frameInfo, text = "Save", command = gridSave).grid(column = 0, row = 2)
ttk.Button(frameInfo, text = "Load", command = gridLoad).grid(column = 0, row = 3)
ttk.Button(frameInfo, text = "Erase All", command = gridErase).grid(column = 0, row = 4)
# Call the root

root.mainloop()