Python 3.x 如何在tkinter中添加多个条目?

Python 3.x 如何在tkinter中添加多个条目?,python-3.x,tkinter,Python 3.x,Tkinter,我对特金特非常陌生。我一直在尝试创建一些基本上可以计算输入分数平均值的东西。我试图给用户选择主题数量的选项,并相应地创建那么多条目 from tkinter import * root = Tk() x, y, d = 0, 0, {} for i in range(1, int(input('Enter no of subjects')) + 1): sub1 = Entry(root, width=15, borderwidth=5) sub1.grid(row=x, col

我对特金特非常陌生。我一直在尝试创建一些基本上可以计算输入分数平均值的东西。我试图给用户选择主题数量的选项,并相应地创建那么多条目

from tkinter import *
root = Tk()
x, y, d = 0, 0, {}
for i in range(1, int(input('Enter no of subjects')) + 1):
    sub1 = Entry(root, width=15, borderwidth=5)
    sub1.grid(row=x, column=y)
    max1 = Entry(root, width=15, borderwidth=5)
    max1.grid(row=x, column=y+2)
    sub1label = Label(root, text='Marks attained', bg='grey', fg='white')
    sub1label.grid(row=x, column=y+1)
    max_sub1label = Label(root, text='Max Marks', bg='grey', fg='white')
    max_sub1label.grid(row=x, column=y+3)
    x += 1

root.mainloop()

是否有方法存储每次输入的数据,以便计算获得的百分比?或者我可以使用其他方法吗?

您可以将值存储在列表中,然后稍后使用所需的值对列表进行索引,并获取所需的项目。这是您更正的代码:

from tkinter import *

root = Tk()

def show():
    for i in range(tot_sub): # Loop through the number of subjects
        attained = attained_marks[i].get() # Get the indexed item from the list and use get()
        max_mark = max_marks[i].get()
        print(f'Attained marks: {attained}, Max marks: {max_mark}') # Print the values out

attained_marks = [] # Empty list to populate later
max_marks = [] # Empty list to populate later
tot_sub = int(input('Enter no. of subjects: ')) # Number of subjects
for i in range(tot_sub):
    sub1 = Entry(root, width=15, borderwidth=5)
    sub1.grid(row=i, column=0)
    attained_marks.append(sub1) # Append each entry to the list

    max1 = Entry(root, width=15, borderwidth=5)
    max1.grid(row=i, column=2)
    max_marks.append(sub1) # Append each entry to the list
    
    sub1label = Label(root, text='Marks attained', bg='grey', fg='white')
    sub1label.grid(row=i, column=1, padx=5)
    
    max_sub1label = Label(root, text='Max Marks', bg='grey', fg='white')
    max_sub1label.grid(row=i, column=3, padx=5)

root.bind('<Return>',lambda e: show()) # For demonstration of getting all the data

root.mainloop()
从tkinter导入*
root=Tk()
def show():
对于范围内的i(tot#u sub):#循环检查受试者的数量
已获得=已获得标记[i]。get()#从列表中获取索引项并使用get()
max_mark=max_marks[i].get()
打印(f'Acreated marks:{Acreated},Max marks:{Max_mark}')#打印出值
获得的_分数=[]#空列表稍后填充
max_marks=[]空列表,稍后填充
tot_sub=int(输入('输入受试者人数:')#受试者人数
对于范围内的i(tot_sub):
sub1=条目(根,宽度=15,边框宽度=5)
子1.网格(行=i,列=0)
获得分数。附加(子1)#将每个条目附加到列表中
max1=条目(根,宽度=15,边界宽度=5)
max1.grid(行=i,列=2)
max_marks.append(sub1)#将每个条目追加到列表中
sub1label=标签(根,text='Marks',bg='grey',fg='white')
子1标记网格(行=i,列=1,padx=5)
max_sub1label=标签(根,text='max Marks',bg='grey',fg='white')
最大标签网格(行=i,列=3,padx=5)
root.bind(“”,lambda e:show())#用于演示如何获取所有数据
root.mainloop()
我还对循环做了一些更改,因为您不需要初始化
x、y、d
等等,因为它可以从循环本身内部轻松实现。我还扩展了代码,因此您可以轻松理解。另外,我不建议使用
input()
,因为它是用于终端的,所以请使用
条目


或者:您也可以使用
dict
并避免使用两个列表,字典将类似于
{'Alternative Marks':[att1,att2,…],'Maximum Mark':[max1,max2,…]}
,但这将使循环和索引更为冗长。

非常感谢。代码真的帮助了我。您能解释一下*args在函数show()中的作用吗?老实说,在使用
bind
时,我们需要将
event
作为参数传递。这里我只有
*args
,这意味着它将接受许多参数。检查我的最新答案。哦,好的,谢谢你的解释!