Panda93
Panda93

Reputation: 1

Tkinter; get input from certain row/colomn

(I'm using Python 3.5.)
I created a 5x5 grid and want to get the input from a certain row/colomn.
I've never used tkinter before and I'm new in programming.

I created the show button, so each time the user pushes it, I want the input to get printed. The problem is that, because of my two for loops, it only prints out the last element 44 because that's when the loop's finished.
How can I get ALL input without changing the for loops?

sudoku = Tk()

def show_entry_fields():
    print(name[r][i].get())


i = 0  
for i in range(0,5):  
    for r in range(0,5)  
        name[r][i] = Entry(sudoku)  
        name[r][i].grid(row=r,column=i)  

Button(sodoku, text='Quit', command=sodoku.quit).grid(row=5, column=1)
Button(sodoku, text='Show', command=show_entry_fields).grid(row=5, column=2)
sodoku.mainloop()

Upvotes: 0

Views: 673

Answers (1)

sami ghazouane
sami ghazouane

Reputation: 137

store the variable in a 2D list or a dictionary


# dictionary style  
root = {0:Tk()}
n = 5
m = 5
size = (n,m)

for i in range(1,n+1):
    for j in range(1,m+1):
        root[i,j] = Entry(sudoku)
        root[i,j].grid(row = i, column = j)

I'm used to use dictionary to store tkObject* (Frame, Grid, etc ...)

Indeed, its usefull when you want to get all objects or just the root, or just the first level of Frame.

In this case, root[1,*] will give you first line of your grid, when root[0] is the Tk instance.


In fact, you can do it like this too :

 # list style
 root = Tk()
 grid = [[Entry(sudoku).grid(row=i,column=j) for i in range(5)] for j in range(5)]

I haven't test this code. So, be careful.


Test this :

import tkinter as tk
root = tk.Tk()
grid = [[tk.Entry().grid(row=i,column=j) for i in range(5)] for j in range(5)]
input()

Upvotes: 2

Related Questions