如何使用入口小部件在tkinter中制作电子表格



我正在制作一个应用程序,用户可以使用tkinter中制作的电子表格来更改数据库中的值,但我不知道如何做到这一点。我用while循环创建了输入框,如下所示:

while o<numberofstudents:
global eTrial
eTrial = Entry(rootOC,width=3)
eTrial.place(x=200+y*43, y=50 + 25 * o)
eTrial.delete(0, "end")
eTrial.insert(0, "100")
o=o+1

如何从用for循环创建的每个单独的输入框中获取数据。我试过.get((,但它似乎不起作用

有没有更有效的方法来做到这一点,或者至少有一个解决方案?

eTrial仅指向已创建的最后一个Entry。根据你想做什么,有很多方法可以做到这一点。下面我将eTrial作为一个列表,并添加了一个Button来将列表的当前内容打印到终端。

import tkinter as tk
o = 0
numberofstudents = 5
columns = 3
rootOC = tk.Tk()
frame = tk.Frame( rootOC )
frame.grid()
widgets = []
for r in range( 1, 1+numberofstudents ):
wid_row = []
for col in range( columns ):
obj = tk.Entry( frame,width=3 )
obj.grid( row = r, column = col )
obj.delete(0, "end")
obj.insert(0, str( 100*r+col ))
wid_row.append( obj )
widgets.append( wid_row )
def onclick():
for row in widgets:
for item in row:
print( item.get(), end = '  ' )
print()
tk.Button( rootOC, text = 'Print input', command = onclick ).grid()
rootOC.mainloop()

如果我在GUI打开后立即点击"打印输入",我会得到:

100  101  102  
200  201  202  
300  301  302  
400  401  402  
500  501  502  

最新更新