I'm new to programming, and I've been trying to solve this problem for a long time (for an online cycling manager game) but I can't. I want to create a table with entries, where a value appears in each cell, but that can be modified by the user later. For a single cell I manage to do it, but when doing the loop, the most I get is that all the cells have the same value (the last one in the list). Most likely it's easy to find the problem :P
from tkinter import *
root=Tk()
frameTabla=Frame(root)
frameTabla.pack()
#creo el valor para introducir en los Entry
valor=StringVar()
#"y" es el nombre de las columnas
y=["Nombre","Edad","Res","Spr","Mon","LLa","Pavés","Tec","Des","Col","Agres","TT","Teq","Exp","€","Salud","Form","Carreras","Puntos"]
#"valores" es el nombre, edad, etc del ciclista
valores=["Byers",23,76,50,11,82,100,73,67,95,12,9,51,21,1037,86,99,13,198]
filas = 1 #una fila sola, para probar solo 1 ciclista de momento
columnas = len(y)
#Inserto los nombres de las columnas con Label (la 1ª más ancha para el nombre)
for j in range(columnas):
if j==0:
Label(frameTabla,width=10,text=y[j],justify="center").grid(row=0,column=j)
else:
Label(frameTabla,width=3,text=y[j],justify="center").grid(row=0,column=j)
#y ahora intento introducir los datos
for i in range(filas):
for j in range(columnas):
if j==0:
valor.set(valores[j])
Entry(frameTabla,width=20,justify="center",textvariable=valor).grid(row=i+1, column=j)
else:
valor.set(valores[j])
Entry(frameTabla,width=4,justify="center",textvariable=valor).grid(row=i+1, column=j)
root.mainloop()
And as I already mentioned, the result is a table where all the cells have the number 198, the last of the values.
The problem is in the variable
valor
, you must recreate it every time you call it:valor=StringVar()
because for each round it must reset its value, being this way: