Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select all text in a Text widget using Python 3 with tkinter

I'm working on my first Python program and have little idea what I'm doing. I want to re-bind ctrl-a (control a) to select all text in a Text widget. The current binding is ctrl-/ (control /). The binding part jumps right to the function but the actual text selection doesn't work. Instead, the cursor jumps to the first character on the first line (like it should) and nothing else happens. I'm sure this is embaressingly easy to fix but after spending hour an hours on it, I can't figure out what's wrong.

Python 3, Windows:

from tkinter import *

# Select all the text in textbox (not working)
def select_all(event):
    textbox.tag_add(SEL, "1.0", END)
    textbox.mark_set(INSERT, "1.0")
    textbox.see(INSERT)

# Open a window
mainwin = Tk()

# Create a text widget
textbox = Text(mainwin, width=40, height=10)
textbox.pack()

# Add some text
textbox.insert(INSERT, "Select some text then right click in this window")

# Add the binding
textbox.bind("<Control-Key-a>", select_all)

# Start the program
mainwin.mainloop()
like image 217
Dave Brunker Avatar asked Dec 10 '12 13:12

Dave Brunker


People also ask

How do I get text from a text widget?

We can get the input from the user in a text widget using the . get() method. We need to specify the input range which will be initially from 1.0 to END that shows the characters starting and ending till the END.

How do you select all text in Python?

The coding of the Select All feature is complete. To try it out, add some text to the text widget and then click on the menu item, Select All or use the Ctrl + A (accelerator shortcut key).


2 Answers

So the new code is...

from tkinter import *

# Select all the text in textbox
def select_all(event):
    textbox.tag_add(SEL, "1.0", END)
    textbox.mark_set(INSERT, "1.0")
    textbox.see(INSERT)
    return 'break'

# Open a window
mainwin = Tk()

# Create a text widget
textbox = Text(mainwin, width=40, height=10)
textbox.pack()

# Add some text
textbox.insert(INSERT, "Select some text then right click in this window")

# Add the binding
textbox.bind("<Control-Key-a>", select_all)
textbox.bind("<Control-Key-A>", select_all) # just in case caps lock is on

# Start the program
mainwin.mainloop()

and yes it works flawlessly. Thank you, very much Bryan Oakley. Steven Rumbalski: that's a VERY good point, I've followed your advice as well.

like image 102
Dave Brunker Avatar answered Sep 16 '22 14:09

Dave Brunker


You need to both do the selection and then inhibit the default action by having your function return the string "break".

This is due to how Tkinter processes events. It uses what it calls "bind tags". Even though it looks like you are binding to a widget, you are actually binding to a tag that is the name of the widget. There can also be bindings to the widget class, to the toplevel window that the widget is in, and the tag "all" (plus, you can invent your own tags if you wish).

The default ordering of these tags is from most-specific to least-specific, and events are processed in that order. Meaning, if you have a binding both on the widget (most specific) and the class (less specific), the binding will fire for the widget first, and then for the class binding (and then for the toplevel, and then for "all").

What this means is that by default, a binding on a widget augments rather than replaces a default binding. The good news is, you can inhibit any further bindings from firing by simply returning the string "break", which stops the chain of bindings from doing any more work.

like image 24
Bryan Oakley Avatar answered Sep 19 '22 14:09

Bryan Oakley