1

I want to do a grid 9*9 like a sudoku grid (with thick and thin line) in tkinter. So I think the best way is to do it with Canvas and create rectangles or lines. But I want each rectangle to contains entry widget to store data.. How can I do this? with a Canvas or a entry grid? (but the latter does not have colors to color line (not rectangle))

Thank u EDIT:I want to color and have entry widget in each square

I want this output with entry widget

Béa
  • 101
  • 2
  • 9

1 Answers1

0

Try something like this:

import tkinter as tk


class SudokuGUI:
    def __init__(self, font_size:int=30):
        self.root = tk.Tk()
        self.chunks = []
        self.frames_list = []
        self.font_size = font_size
        for i in range(3):
            for j in range(3):
                self.create_chunk(i, j)

    def create_chunk(self, row:int, column:int) -> None:
        frame = tk.Frame(self.root, bd=3)
        frame.grid(row=row, column=column)
        self.frames_list.append(frame)

        chunk = []
        for row_number in range(3):
            for column_number in range(3):
                entry = tk.Entry(frame, width=2, justify="center",
                                 font=("", self.font_size),
                                 disabledforeground="black")
                entry.grid(row=row_number, column=column_number)
                chunk.append(entry)
        self.chunks.append(chunk)

    def set(self, chunk:int, cell:int, value=None, **kwargs) -> None:
        if value is not None:
            self.chunks[chunk][cell].delete(0, "end")
        self.chunks[chunk][cell].insert("end", value)
        self.chunks[chunk][cell].config(**kwargs)

    def mainloop(self) -> None:
        self.root.mainloop()


app = SudokuGUI()
app.set(chunk=5, cell=5, value=1, state="disabled")
app.mainloop()

I divided the board into chunks of 9 entries. I added the .set method with sets the state/value of a given cell in a given chunk. You can use it to set the values given. It also disables the user input in those entries.

TheLizzard
  • 7,248
  • 2
  • 11
  • 31