我在Tkinter Gui中有下表,我想知道如何能够获得包含每个单元格中所有值的dict以及dict中每个值的相应row和col 。这是代码:
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()
答案 0 :(得分:2)
下面是一个最小的示例,它将Entry
字段创建为二维列表,然后将其中一个值放入标签中:
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()
答案 1 :(得分:1)
我建议您将表存储在字典中,并使用内置的pickle
模块加载并保存它。
这是我用来创建初始表字典的代码,并且#34; pickle it"用于测试(在实际使用中,您可能希望将其初始化为空值或零值):
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')
输出显示表格中的值:
{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
然后,您可以使用App.__init__()
方法在pickled文件中加载数据,并按如下所示使用它:
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)
应用运行的屏幕截图。请注意,显示的值与首次创建表时打印的值相同。
更新表中的值后,如果您希望通过最初创建pickle table的代码中使用的相同pickle.dump()
函数轻松更新关联文件。