Reputation: 1010
I have the following table in a Tkinter Gui and I wanted to know how I would be able to get a dict with all of the values in each of the cells and the corresponding row and col of each value in the dict as well. Here is the code:
import tkinter as Tkinter
import random
class App:
def __init__(self, master):
frame = Tkinter.Frame(master)
self.data_readout = Tkinter.Button(frame, text="Collect Data", bd=10, height=2, width=10, command=lambda: self.dataReadout(self.table_values))
self.data_readout.grid(row=0, column=0, padx=5, pady=5)
self.table_values = Tkinter.LabelFrame(frame, text="Values", borderwidth=10, relief=Tkinter.GROOVE, padx=10, pady=10)
self.table_values.grid(row=1, column=0, padx=20, pady=20)
for i in range(4): #Rows
for j in range(10): #Columns
b = Tkinter.Entry(self.table_values, text="", width=5)
b.grid(row=i, column=j)
b.insert(0, str(round(random.random()*100)))
frame.grid(row=0, column=0, padx=20, pady=20)
def dataReadout(self, frame):
#returns Dict of row and column
pass
if __name__ == "__main__":
root = Tkinter.Tk()
app = App(root)
root.mainloop()
Upvotes: 0
Views: 2312
Reputation: 123473
I suggest you store the table in a dictionary and use the built-in pickle
module to load and save it.
Here's the code I used to create a initial table dictionary and "pickle it" for testing (in actual use you might want to initialize it to empty or zero values):
import pickle
from pprint import pprint
import random
# Create table
table = {}
filename = 'table.pkl'
height = 4
width = 10
for i in range(height):
table[i] = [round(random.random()*100) for _ in range(width)]
with open(filename, 'wb') as output:
pickle.dump(table, output, pickle.HIGHEST_PROTOCOL)
pprint(table)
print('table created')
Output showing values in table:
{0: [75, 68, 51, 27, 3, 99, 50, 33, 99, 63],
1: [92, 79, 79, 66, 41, 18, 57, 9, 45, 75],
2: [34, 75, 67, 92, 38, 93, 28, 76, 75, 38],
3: [20, 10, 43, 48, 39, 16, 34, 75, 80, 16]}
table created
You can then load the data in the pickled file in the App.__init__()
method and use it as shown:
class App:
def __init__(self, master):
frame = Tkinter.Frame(master)
self.data_readout = Tkinter.Button(frame,
text="Collect Data", bd=10, height=2, width=10,
command=lambda: self.dataReadout(self.table_values))
self.data_readout.grid(row=0, column=0, padx=5, pady=5)
self.table_values = Tkinter.LabelFrame(frame,
text="Values", borderwidth=10,
relief=Tkinter.GROOVE, padx=10, pady=10)
self.table_values.grid(row=1, column=0, padx=20, pady=20)
# Read table.
with open('table.pkl', 'rb') as infile:
table = pickle.load(infile)
# Use table.
height = 4
width = 10
for i in range(height): #Rows
for j in range(width): #Columns
b = Tkinter.Entry(self.table_values, text="", width=5)
b.grid(row=i, column=j)
b.insert(0, str(table[i][j]))
frame.grid(row=0, column=0, padx=20, pady=20)
def dataReadout(self, frame):
rowArr = []
colArr = []
info = frame.children.values()
for children in frame.children.values():
info = children.grid_info()
print(info)
for i in info:
print(i)
Screenshot of app running. Note that the values being displayed are the same as those that were printed when the table was first created.
After the values in table are updated, if you wish you can easily update the associated file by via the same pickle.dump()
function that was used in the code that initially created the pickled table.
Upvotes: 1
Reputation: 15335
Below is a minimal example that creates the Entry
fields as a two-dimensional list, and later puts one of the values in a label:
import tkinter as Tkinter
import random
class App:
def __init__(self, master):
self.b = list()
for i in range(4): #Rows
self.b.append(list()) # to add the 2nd dimension to ith element
for j in range(10): #Columns
self.b[i].append(Tkinter.Entry(master, text="", width=5)) # b[i][j]
self.b[i][j].grid(row=i, column=j)
self.b[i][j].insert(0, str(round(random.random()*100)))
if __name__ == "__main__":
root = Tkinter.Tk()
app = App(root)
Tkinter.Label(root, text=app.b[3][7].get()).grid()
root.mainloop()
Upvotes: 3