I'm developing a GUI using python 3.6 but I need the user to double-click on a tkinter Entry
widget to allow input (to prevent modification of fields by accident), instead of just press any key to input text.
Entry
?I've tried by first to override events with the methods below to unbind keystrokes but none of them worked, so re-definition of new bind (double click) is not yet implemented.
Entry.unbind_all('<Key>')
Entry.unbind_all('<KeyPress>')
Entry.unbind_all('<KeyRelease>')
One simple way of doing preventing the user input by keystrokes until a double-click, is simply manipulating the state of an Entry
with double click and focus out events. As in by default every widget is read-only, when double-clicked a single one gets enabled, and when lost focus it's read-only again:
try: # In order to be able to import tkinter for
import tkinter as tk # either in python 2 or in python 3
except ImportError:
import Tkinter as tk
def on_double_click(widget):
widget['state'] = 'normal'
def on_lose_focus(widget):
widget['state'] = 'readonly'
def main():
root = tk.Tk()
entries = list()
for i in range(3):
entries.append(tk.Entry(root, state='readonly'))
entries[-1].bind('<Double-Button-1>',
lambda e, w=entries[-1]: on_double_click(w))
entries[-1].bind('<FocusOut>',
lambda e, w=entries[-1]: on_lose_focus(w))
entries[-1].pack()
tk.mainloop()
if __name__ == '__main__':
main()