I am trying to build a sudoku game. I got problem with the GUI. The game is consisted of 9 blocks, each block has 9 cells. But I can only get the last 3 blocks. I miss the first 6 rows. This is the result I got:
The code is as follow:
import tkinter as tk
root = tk.Tk()
# Create the puzzle
puzzle = tk.Frame(root, bg='white')
puzzle.pack()
# Add the 3 * 3 big blocks
blocks = [[None] * 3] * 3
for i in range(3):
for j in range(3):
blocks[i][j] = tk.Frame(puzzle, bd=1, highlightbackground='light blue',
highlightcolor='light blue', highlightthickness=1)
blocks[i][j].grid(row=i, column=j, sticky='nsew')
# Add the 9 * 9 cells
btn_cells = [[None] * 9] * 9
for i in range(9):
for j in range(9):
# Add cell to the block
# Add a frame so that the cell can form a square
frm_cell = tk.Frame(blocks[i // 3][j // 3])
frm_cell.grid(row=(i % 3), column=(j % 3), sticky='nsew')
frm_cell.rowconfigure(0, minsize=50, weight=1)
frm_cell.columnconfigure(0, minsize=50, weight=1)
var = tk.StringVar()
btn_cells[i][j] = tk.Button(frm_cell, relief='ridge', bg='white', textvariable=var)
btn_cells[i][j].grid(sticky='nsew')
# Show the index for reference
var.set(str((i, j)))
root.mainloop()
Any help is appreciated.
CodePudding user response:
The problem is
blocks = [[None] * 3] * 3
it doesn't create 3 unique sublists but it creates 3 reference to the same list.
It should be
blocks = [[None for x in range(3)] for x in range(3)]
I would also use
btn_cells = [[None for x in range(9)] for x in range(9)]
Frankly, I would write it in different way - using apppend()
blocks = []
for r in range(3): # `r` like `row`
row = []
for c in range(3): # `c` like `column`
frame = tk.Frame(puzzle, bd=1, highlightbackground='light blue',
highlightcolor='light blue', highlightthickness=1)
frame.grid(row=r, column=c, sticky='nsew')
row.append(frame)
blocks.append(row)