0

So i have been asked to create a sudoku simulator for my A-Level assessment, with a GUI in Tkinter. I have managed to create a 9x9 grid of buttons, however i would like every 3rd line to be bold(A), or have every 3x3 group of buttons a different colour(B). Below are images of what i had in mind.

B A

Here is my code.

from tkinter import *

#Create & Configure root 
root = Tk()
Grid.rowconfigure(root, 0, weight=1)
Grid.columnconfigure(root, 0, weight=1)
root.resizable(width=False, height=False)

#Create & Configure frame 
frame=Frame(root, width=900, height = 900)
frame.grid(row=0, column=0, sticky=N+S+E+W)


#Create a 9x9 (rows x columns) grid of buttons inside the frame
for row_index in range(9):
    Grid.rowconfigure(frame, row_index, weight=1)
    for col_index in range(9):
        Grid.columnconfigure(frame, col_index, weight=1)
        btn = Button(frame, width = 12, height = 6) #create a button inside frame 
        btn.grid(row=row_index, column=col_index, sticky=N+S+E+W)


root.mainloop()

Any help would be greatly appreciated!

PLEASE NOTE: I do later intend to add numbers to each button and make it possibile to play a Sudoku game, so please bear that in mind when creating solutions. Any help on how i could assign each button a number efficiently (in a for loop for instance) would also be appreciated!!

Callum Twig
  • 65
  • 2
  • 9

1 Answers1

0

Here's an MCVE which demonstrates the way to colour the buttons as you go:

import tkinter as tk

root = tk.Tk()

for row_index in range(9):
    for col_index in range(9):
        if (row_index in {0, 1, 2, 6, 7, 8} and col_index in {3, 4, 5}) or \
                (row_index in {3, 4, 5} and col_index in {0, 1, 2, 6, 7, 8}):
            colour = 'black'
        else:
            colour = None
        button = tk.Button(root, width=1, height=1, bg=colour)
        button.grid(row=row_index, column=col_index, sticky='nswe')

root.mainloop()

...when it comes to assigning the numbers, I'd leave it up to you to come up with a system.

adder
  • 3,512
  • 1
  • 16
  • 28