Marco
Marco

Reputation: 515

How to create a large grid of entries in Tkinter effitienly?

I want to create a Sudoku solver in tkinter as practice and I am trying to figure out how to actually create the grid in order for the user to input the sudoku board. I was thinking of using Entries for this but I would have to do 9x9 = 81 of them in total.

e1 = Entry(master)
e2 = Entry(master)
e3 = Entry(master)
     . . . 
e81 = Entry(master)


e1.grid(row=0, column = 0)
e2.grid(row=0, column = 1)
ae33.grid(row=0, column = 2)
       . . . 
e81.grid(row=9, column = 9)

There has to be another way of doing this right?

Upvotes: 2

Views: 392

Answers (1)

Reblochon Masque
Reblochon Masque

Reputation: 36682

You can use a for loop to populate the entry fields, and store them in a data structure:

entries = [[None for col in range(9)] for row in range(9)]

for row in range(9):
    for col in range(9):
        e = tk.Entry(master)
        e.grid(row=row, column=col)
        entries[row][col] = e

you can then access the entries with a row and column index.

Upvotes: 2

Related Questions