Jamie Truong
Jamie Truong

Reputation: 7

Is there a way of getting data from entries by rows from a table of entries in Tkinter?

So far I have a portion of code for a function that works fine as is for generating a table off of user input and then getting data from the table generated to be used in a line graph. However, the solution as it stands creates one massive list by iterating through every entry's data and is then graphed as one massive line graph. I intended for the function to create lists from each row of the table which is then inserted into a master list for pyplot to then graph as multiple lines on the same graph. Is there a way to achieve this? This is the code I am using:

import tkinter as tk
import matplotlib.pyplot as plt
graphinput = tk.Tk()
def opentable():
    global total_rows
    global total_columns
    total_rows = int(yaxis.get())
    total_columns = int(xaxis.get())
    table = tk.Toplevel(graphinput)
    table.resizable(0,0)
    table.title("Table")
    def tcompile():
        masterlines = []   
        for cell in my_entries:
            print(cell.get())
            masterlines.append(int(cell.get()))
        plt.plot(masterlines)
        plt.show()
    my_entries = []
    for i in range(total_rows):
        for j in range(total_columns):
            cell = tk.Entry(table,width=20,font=('Agency FB',15))
            cell.grid(row=i,column=j)
            my_entries.append(cell)
    tblframe = tk.Frame(table,bd=4)
    tblframe.grid(row=i+1,column=j)
    compbtn = tk.Button(tblframe,font=("Agency FB",20),text="Compile",command=tcompile)
    compbtn.grid(row=0,column=0)
tablegrid = tk.Frame(graphinput,bd=4)
tablegrid.pack()
xlabel = tk.Label(tablegrid,text="Column Entry")
xlabel.grid(row=0,column=0)
ylabel = tk.Label(tablegrid,text="Row Entry")
ylabel.grid(row=0,column=1)
xaxis = tk.Entry(tablegrid)
xaxis.grid(row=1,column=0)
yaxis = tk.Entry(tablegrid)
yaxis.grid(row=1,column=1)
framebtn = tk.Button(tablegrid,text="Create",command=opentable)
framebtn.grid(row=3,column=0)
graphinput.mainloop()

Upvotes: 0

Views: 708

Answers (1)

TheLizzard
TheLizzard

Reputation: 7680

Try something like this:

import tkinter as tk


def tcompile():
    masterlines = []
    for i in range(total_rows):
        row = []
        for j in range(total_columns):
            data = my_entries[i*total_columns+j].get()
            # data = int(data) # Removed for testing purposes
            row.append(data)
        masterlines.append(row)
    print(masterlines)


root = tk.Tk()

total_rows = 3
total_columns = 4

my_entries = []
for i in range(total_rows):
    for j in range(total_columns):
        cell = tk.Entry(root)
        cell.grid(row=i, column=j)
        my_entries.append(cell)

# Add a button for testing purposes
button = tk.Button(root, text="Click me", command=tcompile)
button.grid(row=1000, columnspan=total_columns)

root.mainloop()

The key is to use my_entries[i*total_columns+j] to get the entry in row i and column j.

Note: I didn't add the matplotlib stuff because I don't really know how it works.

Upvotes: 2

Related Questions