为什么在python中insert或set for tkinter条目字段会非常复杂

2024-09-30 00:38:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个应用程序,它显示一个数据网格,并允许您编辑每条记录的日期。现在,我只有5条记录,每条记录有3个数据点的输入字段。你知道吗

这是相关代码,当屏幕加载时,它只是将保存的数据填充到输入字段中:

start_time = time.time()
for student in sVars:
    scoresexist={"ps":False, "et":False, "v":False}
    for score in studentScores:
        if score['Name']==student['Student Name'] and score['week']==week and score['team']==team and score['section']==section and score['subject']==subject:
            if score['score'] != "":
                scoresexist[score['assignment']]=True
                myscore = score['score']
                setID = student[score['assignment']+"Entry"]
                setID.delete(0,END)
                setID.insert(0,myscore)
                setID.update()
    for key in scoresexist:
        if scoresexist[key]==False:
           student[key+"EntryID"].set("")
print("elapsed time is " + str(time.time() - start_time))   

一旦我开始添加更多的记录,应用程序就开始冻结。经过反复试验,我最终发现,花了这么长时间的线路是

setID.insert(0,myscore)

如果注释掉该行,或者将该行更改为get()而不是set(),则无论尝试的记录数是多少,所用时间都会返回到0.0秒。你知道吗

当我有4个或更少的记录时,这个代码的运行时间大约是0.5秒。 当我转到5条记录时,这个代码的运行时间大约是3秒 当我查到6条记录时,这个代码的运行时间大约是25秒 一旦我有了7条记录,应用程序就冻结了。 setID指的是实际的输入字段 在这次审判之前我把那句话当作

setID.set(myscore)

setID引用了输入字段的textvariable。那条线还是一样慢。你知道吗

哦,还有。。之所以有三条线是因为我在打猎的时候发现了是什么导致了延迟。一开始,我

student[score['assignment']+"Entry"].set(score['score'])

我开始把东西分成几行,直到我发现set(insert now)行花了很长时间。你知道吗

而且,我一开始没有

setID.update()

我补充说,现在我可以看到一次弹出一个字段,并知道计算机正在工作,但它仍然是非常缓慢的完成。然而,即使没有这条线,它仍然是极其缓慢的。你知道吗

我做错了吗?或者,这只是一个问题,你不能使用几个输入字段没有滞后?同样,当我有5条记录时,我有15个输入字段。当我有7条记录并且应用程序冻结时,屏幕上有21个输入字段。你知道吗

编辑:

下面是一个例子,说明

#add to student dictionary
studentVars['psEntryID']=StringVar()
#create field for participation score
studentVars["psEntry"] = tk.Entry(newrow, textvariable=studentVars['psEntryID'])
studentVars["psEntry"].grid(row=0, column=currentcolumn, padx=(10,50))
#change to next column
currentcolumn = currentcolumn + 2



#add to student dictionary
studentVars['etEntryID']=StringVar()             
#create field for exit ticket score
studentVars["etEntry"]  = tk.Entry(newrow, textvariable=studentVars['etEntryID'])
studentVars["etEntry"] .grid(row=0, column=currentcolumn, padx=(10,50))
#change to next column
currentcolumn = currentcolumn + 2


#add to student dictionary
studentVars['vEntryID']=StringVar()   
#create field for vocabulary score
studentVars["vEntry"]  = tk.Entry(newrow, textvariable=studentVars['vEntryID'])
studentVars["vEntry"] .grid(row=0, column=currentcolumn, padx=(10,50))

以及它是如何被追踪的

studentVars['psEntryID'].trace('w',lambda a, b, c, d=studentVars['psEntryID'], e=studentVars['Student Name'], f="ps": calculateStudent(a, b, d, d, e, f))
studentVars['etEntryID'].trace('w',lambda a, b, c, d=studentVars['etEntryID'], e=studentVars['Student Name'], f="et": calculateStudent(a, b, d, d, e, f))
studentVars['vEntryID'].trace('w',lambda a, b, c, d=studentVars['vEntryID'], e=studentVars['Student Name'], f="v": calculateStudent(a, b, d, d, e, f))

更新:

我好像自己也回答了这个问题。答案显然是我在trace上使用了textvariable来捕捉输入字段中的更改。所以,当我加载保存的数据时,每当一个输入字段被更新时,它一定会一遍又一遍地尝试做任何事情。我删除了跟踪线,而是使用bind on

<FocusOut>

现在,我有大约20条记录,它仍在0.11秒内加载整个表。你知道吗


Tags: toname应用程序fortime记录columnstudent

热门问题