There was so many similar examples on SO
import tkinter as tk
def callback(event):
print('e.get():', e.get())
# or more universal
print('event.widget.get():', event.widget.get())
# select text after 50ms
root.after(50, select_all, event.widget)
def select_all(widget):
# select text
widget.select_range(0, 'end')
# move cursor to the end
widget.icursor('end')
root = tk.Tk()
e = tk.Entry(root)
e.pack()
e.bind('<Control-a>', callback)
root.mainloop()
bind
expects filename without ()
and arguments (callback). But also bind
executes this function always with one argument event
which gives access to entry which executed this function event.widget
so you can use it with many different entries. And finally Entry
has .get()
to get all text.
EDIT:
Because after releasing keys <Control-a>
selection is removed so I use after()
to execute selection after 50ms. It selects all text (but it moves cursor to the beginning) and moves cursor to the end. (see code above)
EDIT:
Before I couldn't find correct combination with Release
but it has to be <Control-KeyRelease-a>
and now it doesn't need after()
import tkinter as tk
def callback(event):
print('e.get():', e.get())
# or more universal
print('event.widget.get():', event.widget.get())
# select text
event.widget.select_range(0, 'end')
# move cursor to the end
event.widget.icursor('end')
root = tk.Tk()
e = tk.Entry(root)
e.pack()
e.bind('<Control-KeyRelease-a>', callback)
root.mainloop()