Reputation: 2741
So I'm using Python and Tkinter, I'm making a to-do list. Except, there is a very slow wait time when I press ENTER to running the function.
The Code:
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()
When I run it, it works fine
But, the wait time for adding new tasks after pressing ENTER is terribly slow. But, strangely, if I use the left mouse click for the binding part instead of the enter key, it works fine responding instantly?
Could someone please explain.
Upvotes: 1
Views: 1046
Reputation: 2741
Taken from jasonharper...
<Enter>
is whenever the mouse enters the widget
<Return>
is the actual enter button
Upvotes: 2