在Python Tkinter中输入键绑定的响应非常慢

时间:2018-08-04 23:01:21

标签: python python-3.x performance tkinter

因此,我正在使用Python和Tkinter,这是我的工作清单。除此以外,当我按ENTER键运行该功能时,等待时间非常慢。

代码:

from tkinter import *
import pickle

'''
Final Version
'''


def delete_all():
    first_listbox.delete(0, END)
    while len(tasks) > 0:
        del tasks[0]

def add(event):
    if entry.get().strip() == '':
        return None
    first_listbox.insert(END, entry.get())
    tasks.append(entry.get())
    entry.delete(0, END)


def delete():
    selection = first_listbox.curselection()
    first_listbox.delete(selection)
    tasks.remove(entry.get())

root = Tk()

frame = Frame(root, bg='LightBlue')
frame.grid(row=0, column=0, sticky=W, rowspan=10, columnspan=10)


frame.bind('<Enter>', add)

root.geometry('400x400')
root.title('Bad to-do list')
root.configure(bg='LightBlue')

first_listbox = Listbox(frame, width=50, selectmode = BROWSE)
first_listbox.grid(row=0, column=0, rowspan = 5, sticky=W)

entry = Entry(frame, width=50)
entry.grid(row=5, column=0, sticky=W, columnspan=2)

#submit_button = Button(frame, text='Insert', command=add)<-- Old Code Before Binding Enter
#submit_button.grid(row=5, column=1, sticky=W) <-- Old Code Before Binding Enter

delete_button = Button(frame, text='Done', command=delete)
delete_button.grid(row=0, column=1, sticky=W)

delete_all_button = Button(frame, text='Delete All', command=delete_all)
delete_all_button.grid(row=1, column=1, sticky=W, columnspan=2)



pickle_out = open('to-do-example.pickle', 'rb')
tasks = pickle.load(pickle_out)
pickle_out.close()
for item in tasks:
    first_listbox.insert(0, item)




root.mainloop()


pickle_out = open('to-do-example.pickle', 'wb')
pickle.dump(tasks, pickle_out)
pickle_out.close()

当我运行它时,它工作正常

What It Looks Like

但是,按ENTER后添加新任务的等待时间非常慢。 但是,奇怪的是,如果我用鼠标左键单击绑定部分而不是Enter键,它可以立即响应正常吗?

有人可以解释。

1 个答案:

答案 0 :(得分:0)

来自jasonharper ...

<Enter> 

每当鼠标进入小部件时

<Return>

是实际的输入按钮