Python 保存颜色选择器

Python save colorchooser

我有这个类似绘画的程序,还有一个按钮,如果我点击它,我可以选择一种颜色。但是我似乎无法弄清楚如何保存该选项并更改我正在绘制的单元格的颜色。我的默认绘制颜色为“黑色”,轮廓也是黑色,值应更改为 def 颜色保存的值。请帮忙,我第一次做这样的事情。

from tkinter import *
from tkinter import colorchooser
import tkinter as tk

# root = Tk()


class Grilla:

    colorCelda = "black"
    colorDefault = "white"
    colorBorde = "black"
    bordeDefault = "black"

    def __init__(self, root, master, x, y, size):
        """ Constructor of the object called by Cell(...) """
        self.master = master
        self.abs = x
        self.ord = y
        self.size = size
        self.fill = False

    def switch(self):
        """ Switch if the cell is filled or not. """
        self.fill = not self.fill

    def reset(self):
        """ Clears the cell """
        self.fill = False

    def draw(self):
        # dibujar en el canvas
        if self.master is not None:

            outline = Grilla.colorBorde
            fill = Grilla.colorCelda

            if not self.fill:
                outline = Grilla.bordeDefault
                fill = Grilla.colorDefault

            xmin = self.abs * self.size
            xmax = xmin + self.size
            ymin = self.ord * self.size
            ymax = ymin + self.size

            self.master.create_rectangle(xmin, ymin, xmax, ymax, fill=fill, outline=outline)


class CellGrilla(Canvas):
    def __init__(self, master, numFil, numCol, tamGrid, *args, **kwargs):
        Canvas.__init__(self, master, width=tamGrid * numCol, height=tamGrid * numFil, *args, **kwargs)
        self.bind("<Button-1>", self.square_clicked)
        self.cellSize = tamGrid

        self.pen = "draw"

        self._grid = []
        for row in range(numFil):

            line = []
            for column in range(numCol):
                line.append(Grilla(master, self, column, row, tamGrid))

            self._grid.append(line)

        # memorize the cells that have been modified to avoid many switching of state during mouse motion.
        self.switched = []

        self.draw()

    def square_clicked(self, event):
        row, column = self._coordenadas(event)
        cell = self._grid[row][column]
        if self.pen == "draw":
            cell.switch()
        cell.draw()

    def draw(self):
        for row in self._grid:
            for cell in row:
                cell.draw()

    def _coordenadas(self, event):
        # `int(a / b)` is the same as `a // b`
        row = event.y // self.cellSize
        column = event.x // self.cellSize
        return row, column

    def switch_to_draw(self):
        self.pen = "draw"

    def color(self):
        colorSelec = colorchooser.askcolor()[1]

    def DDA(self):
        # DDA algorithm 
        
    def clear(self):
        for row in self._grid:
            for cell in row:
                cell.reset()
                cell.draw()


if __name__ == "__main__":
    app = Tk()

    # Tamaño de canvas x tamaño de pixeles
    grid = CellGrilla(app, 75, 75, 10)

    grid.grid(row=1, column=1, rowspan=4, sticky="news")

    colorBoton = Button(app, text="Elegir Color", command=grid.color, height=1, width=30)
    ddaBoton = Button(app, text="DDA", command=grid.DDA, height=1, width=30)
    zoomInBoton = Button(app, text="Zoom in", command=grid.switch_to_draw, height=1, width=30)
    zoomOutBoton = Button(app, text="Zoom out", command=grid.switch_to_draw, height=1, width=30)
    reset_btn = Button(app, text="Borrar", command=grid.clear, height=1, width=30)

    zoomInBoton.grid(row=1, column=2, sticky="news")
    zoomOutBoton.grid(row=2, column=2, sticky="news")
    colorBoton.grid(row=3, column=2, sticky="news")
    ddaBoton.grid(row=4, column=2, sticky="news")
    reset_btn.grid(row=5, column=2, sticky="news")

    app.mainloop()

如果您选择了颜色,则需要更新Grilla.colorCelda

class CellGrilla(Canvas):
    ...
    def color(self):
        colorSelec = colorchooser.askcolor()[1]
        if colorSelec:
            Grilla.colorCelda = colorSelec