有没有一种方法可以从 Tkinter 中的 table 个条目中逐行获取数据?
Is there a way of getting data from entries by rows from a table of entries in Tkinter?
到目前为止,我有一部分函数代码可以正常工作,因为它可以根据用户输入生成 table,然后从生成的 table 中获取数据以用于线形图。但是,目前的解决方案通过遍历每个条目的数据来创建一个巨大的列表,然后将其绘制为一个巨大的折线图。我打算让函数从 table 的每一行创建列表,然后将其插入到 pyplot 的主列表中,然后在同一张图上绘制成多条线。有没有办法做到这一点?这是我使用的代码:
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()
尝试这样的事情:
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()
关键是使用my_entries[i*total_columns+j]
获取第i
行第j
列的条目。
注意:我没有添加 matplotlib
东西,因为我真的不知道它是如何工作的。
到目前为止,我有一部分函数代码可以正常工作,因为它可以根据用户输入生成 table,然后从生成的 table 中获取数据以用于线形图。但是,目前的解决方案通过遍历每个条目的数据来创建一个巨大的列表,然后将其绘制为一个巨大的折线图。我打算让函数从 table 的每一行创建列表,然后将其插入到 pyplot 的主列表中,然后在同一张图上绘制成多条线。有没有办法做到这一点?这是我使用的代码:
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()
尝试这样的事情:
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()
关键是使用my_entries[i*total_columns+j]
获取第i
行第j
列的条目。
注意:我没有添加 matplotlib
东西,因为我真的不知道它是如何工作的。