如何从我在 Python Tkinter 中生成的 table 个条目中获取数据?
How do I get data from a table of entries I've generated in Python Tkinter?
我对在 python 中使用 Tkinter 很陌生,我打算在我的软件解决方案中使用它创建一个 table 生成函数,我发现问题是找到一种方法来调用打开并获取条目 table 内的数据以放入列表中,然后使用 matplotlib 从 table 生成图表。到目前为止,我已经尝试使用 for 循环遍历我用来生成 table 的同一个变量,以便从条目中获取数据,但这只会给出右下角条目的值table 仅此而已。同样,我只看到类似主题的指南只展示了如何在条目中显示数据而不是获取它们。
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)
def tcompile():
masterlines = []
for entries 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()
现在 tcompile
单元格中指的是最后创建的条目。当您遍历条目列表 (my_entries
) 时,您将每个条目保存在一个名为 entries
而不是 cell
的变量中。所以我建议你改成for entries in my_entries
=> for cell in my_entries
。这样每个条目都将保存在 cell
中,因此您可以调用 cell.get()
.
我对在 python 中使用 Tkinter 很陌生,我打算在我的软件解决方案中使用它创建一个 table 生成函数,我发现问题是找到一种方法来调用打开并获取条目 table 内的数据以放入列表中,然后使用 matplotlib 从 table 生成图表。到目前为止,我已经尝试使用 for 循环遍历我用来生成 table 的同一个变量,以便从条目中获取数据,但这只会给出右下角条目的值table 仅此而已。同样,我只看到类似主题的指南只展示了如何在条目中显示数据而不是获取它们。
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)
def tcompile():
masterlines = []
for entries 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()
现在 tcompile
单元格中指的是最后创建的条目。当您遍历条目列表 (my_entries
) 时,您将每个条目保存在一个名为 entries
而不是 cell
的变量中。所以我建议你改成for entries in my_entries
=> for cell in my_entries
。这样每个条目都将保存在 cell
中,因此您可以调用 cell.get()
.