简体   繁体   中英

Trying to create a 9 x 9 sudoku grid in tkinter, but the padding (padx) for every ninth square is off. What is the problem in my code?

This is all the code so far. I have tried changing the padding values, the if statement conditions, and adding an elif condition for when both (row + 1) % 3 == 0 and (col + 1) % 3 == 0. They all seemed to have no positive effect on the grid.

import tkinter


mW = tkinter.Tk()

mW.title('Sudoku')
mW.geometry('800x640')


def grid_layout(grid_dim):
    entries = []
    for i in range(0, grid_dim ** 2):
        row = i // grid_dim
        col = i % grid_dim
        entries.append(tkinter.Entry(mW, width=3, highlightthickness=1, highlightbackground='#000000'))
        if (row + 1) % 3 == 0:
            entries[i].grid(row=row, column=col, pady=(0, 10), ipadx=5, ipady=5)
        elif (col + 1) % 3 == 0:
            entries[i].grid(row=row, column=col, padx=(0, 10), ipadx=5, ipady=5)
        else:
            entries[i].grid(row=row, column=col, ipadx=5, ipady=5)


grid_layout(9)

mW.mainloop()

The sudoku grid

Problem is because last cell need both pady=(0, 10) and padx=(0, 10) - so use two if instead of if/elif

if (row+1) % 3 == 0:
    entries[i].grid(row=row, column=col, pady=(0, 10), ipadx=5, ipady=5)

if (col+1) % 3 == 0:
    entries[i].grid(row=row, column=col, padx=(0, 10), ipadx=5, ipady=5)
else:
    entries[i].grid(row=row, column=col, ipadx=5, ipady=5)

You can also do it little different - using (0,0) as default value for all cells

pad_y = (0, 0)
pad_x = (0, 0)

if (row+1) % 3 == 0:
    pad_y = (0, 10)

if (col+1) % 3 == 0:
    pad_x = (0, 10)

entries[i].grid(row=row, column=col, ipadx=5, ipady=5, padx=pad_x, pady=pad_y)

EDIT:

I would write it little different - using two for -loops. And skiping pad for last cell in row and last cell in column.

import tkinter as tk

# --- functions ---

def grid_layout(mW, grid_dim):
    entries = []
    
    for row in range(grid_dim):
        for col in range(grid_dim):
        
            entry = tk.Entry(mW, width=3, highlightthickness=1, highlightbackground='#000000')
            
            pad_y = (0, 0)
            pad_x = (0, 0)
            
            if (row+1) % 3 == 0 and (row+1) < grid_dim: # skip for last row
                pad_y = (0, 10)
                
            if (col+1) % 3 == 0 and (col+1) < grid_dim: # skip for last column
                pad_x = (0, 10)

            entry.grid(row=row, column=col, ipadx=5, ipady=5, padx=pad_x, pady=pad_y)

            entries.append(entry)

    return entries
    
# --- main ---

mW = tk.Tk()
mW.title('Sudoku')

entries = grid_layout(mW, 9)  # send result 

mW.mainloop()

在此处输入图像描述

And I send mW to function and entries send back in return because

Explicit is better than implicit.

See: Zen of Python

You need to add:

  • left padding on column 3 and 6
  • top padding on row 3 and 6
def grid_layout(grid_dim):
    entries = []
    for i in range(grid_dim*grid_dim):
        row = i // grid_dim
        col = i % grid_dim
        entries.append(tkinter.Entry(mW, width=3, highlightthickness=1, highlightbackground='#000000'))
        padx = (10, 0) if col in (3, 6) else None
        pady = (10, 0) if row in (3, 6) else None
        entries[i].grid(row=row, column=col, padx=padx, pady=pady, ipadx=5, ipady=5)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM