Python Forum
Grid data entry problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Grid data entry problem
#1
Hello all,
I am trying to write a program to enter data into a grid.
When I try printing the results of the data entry, it only prints the last row.
If somebody can find the problem, it would be appreciated.
Below is the code.
Thanks in advance.

from tkinter import *
from tkinter import ttk

def data_table(frame3):
    for i in range(3):
        values[i][0] = Entry(frame3)
        values[i][0].grid(row=i,  column= 0)

        values[i][1] = Entry(frame3)
        values[i][1].grid(row=i,  column= 1)


def main_program():
    var1 = values[0][0].get()
    var2 = values[0][1].get()
    var3 = values[1][0].get()
    var4 = values[1][1].get()
    var5 = values[2][0].get()
    var6 = values[2][1].get()
    print(var1)
    print(var2)
    print(var3)
    print(var4)
    print(var5)
    print(var6)
    print("Finished\n")


if __name__ == "__main__":
    current_file = __file__
    mw=Tk()
    mw.geometry('700x300+400+200')
    mw.title(current_file)

    frame3 = Frame(mw)
    framebot = Frame(mw)

    frame3.pack(side=TOP,fill=X)
    framebot.pack(side=BOTTOM,fill=X)

    btn3 = Button(framebot,text='Create Table',font=("Times",16),command=main_program).pack(side="left")

    values = [[""]*2]*3
    data_table(frame3)
    mw.mainloop()
Reply
#2
Maybe I'm wrong, but where do you have data entry in Entry? Or is not your whole program here?
Reply
#3
You need to build your list more like this:

values = [[0] * 2 for _ in range(3)]

Better yet, modify the data_table function to create and return the table.
from tkinter import *
from tkinter import ttk
 
def data_table(frame, rows, columns):
    table = []
    for r in range(rows):
        row = []
        for c in range(columns):
            entry = Entry(frame)
            entry.grid(row=r, column=c)
            row.append(entry)
        table.append(row)
    return table
 
 
def main_program():
    for r, row in enumerate(values):
        for c, entry in enumerate(row):
            print(f'value[{r},{c}] = {entry.get()}')
    print("Finished\n")
 
 
if __name__ == "__main__":
    current_file = __file__
    mw=Tk()
    mw.geometry('700x300+400+200')
    mw.title(current_file)
 
    frame3 = Frame(mw)
    framebot = Frame(mw)
 
    frame3.pack(side=TOP,fill=X)
    framebot.pack(side=BOTTOM,fill=X)
 
    btn3 = Button(framebot,text='Create Table',font=("Times",16),command=main_program).pack(side="left")
 
    values = data_table(frame3, 3, 2)
    mw.mainloop()
Reply
#4
Thanks deanhystad, your solution works well.
The problem was with the values array. I was initializing it as a 2x6 array of spaces.
The proper way is to initialize it as an empty array and append the entry widgets.
Below is an alternative solution.
Thanks.

from tkinter import *
from tkinter import ttk

def data_table(frame3,values):
    for i in range(3):
        j = 0
        print(i)
        values.append([])   # add row
        values[i].append(Entry(frame3)) # add column 0
        values[i][j].insert(0,str(i)+str(j))
        values[i][j].grid(row=i,  column= 0)
        j += 1

        values[i].append(Entry(frame3)) # add column 1
        values[i][j].insert(0,str(i)+str(j))
        values[i][j].grid(row=i,  column= 1)
        j += 1


def main_program():

    var1 = values[0][0].get()
    var2 = values[0][1].get()
    var3 = values[1][0].get()
    var4 = values[1][1].get()
    var5 = values[2][0].get()
    var6 = values[2][1].get()

    print(var1)
    print(var2)
    print(var3)
    print(var4)
    print(var5)
    print(var6)
    print("Finished\n")


if __name__ == "__main__":
    current_file = __file__
    mw=Tk()
    mw.geometry('700x300+400+200')
    mw.title(current_file)

    frame3 = Frame(mw)
    framebot = Frame(mw)

    frame3.pack(side=TOP,fill=X)
    framebot.pack(side=BOTTOM,fill=X)

    btn3 = Button(framebot,text='Create Table',font=("Times",16),command=main_program).pack(side="left")

#    values = [[""]*2]*3
    values = []
    data_table(frame3,values)

    mw.mainloop()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Trouble getting data from an entry. Oshadha 2 2,475 Feb-05-2021, 03:35 AM
Last Post: steve_shambles
  [Tkinter] Entry problem DPaul 5 3,231 Oct-29-2020, 05:38 PM
Last Post: deanhystad
  How to retreive the grid location of an Entry widget kenwatts275 7 4,596 Apr-24-2020, 11:39 PM
Last Post: Larz60+
  Problem with setting variable within a Entry validatecommand kenwatts275 1 1,885 Mar-31-2020, 01:53 AM
Last Post: deanhystad
  Transfer Toplevel window entry to root window entry with TKinter HBH 0 4,466 Jan-23-2020, 09:00 PM
Last Post: HBH
  Question about data grid for forms - desktop program Everest 5 3,508 Apr-18-2019, 11:04 AM
Last Post: buran
  [Tkinter] how to get the entry information using Entry.get() ? SamyPyth 2 3,495 Mar-18-2019, 05:36 PM
Last Post: woooee
  [Tkinter] Grid Problem Deniz_ozbe 1 2,498 Jul-24-2018, 09:54 PM
Last Post: woooee
  [Tkinter] Problem reading entry box in thread LeeMadeux 2 3,330 Jan-17-2018, 03:15 PM
Last Post: LeeMadeux

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020