Selection in Tkinter Text widget.

A

Ant

Hi all,

I have been trying to select text in a Text widget programmatically. I
have been trying the following minimal example:
#=================================
from Tkinter import *

def showgui():
win = Tk()

area = Text(win, width = 50, height = 20)
area.pack()

new = """Lots of text here
and here
and here..."""
area.insert("1.0", new)

area.tag_add(SEL, "1.0", END)

win.mainloop()

if __name__ == "__main__":
showgui()
#==================================

The area.tag_add(...) line should - from what I have read in Frederik's
Intro to Tkinter guide - select all of the text in the text area. It
doesn't however...

Does anyone have any idea how to get this to work? Or tell me what I am
doing wrong.

Cheers,
 
F

Fredrik Lundh

Ant said:
I have been trying to select text in a Text widget programmatically. I
have been trying the following minimal example:
#=================================
from Tkinter import *

def showgui():
win = Tk()

area = Text(win, width = 50, height = 20)
area.pack()

new = """Lots of text here
and here
and here..."""
area.insert("1.0", new)

area.tag_add(SEL, "1.0", END)

area.focus_set()
win.mainloop()

if __name__ == "__main__":
showgui()
#==================================

The area.tag_add(...) line should - from what I have read in Frederik's
Intro to Tkinter guide - select all of the text in the text area. It
doesn't however...

it does, but by default, the selection is only shown for widgets that has the key-
board focus. if you add an explicit focus_set() call, you'll see the selection.

</F>
 
A

Ant

Fredrik Lundh wrote:
....
it does, but by default, the selection is only shown for widgets that has the key-
board focus. if you add an explicit focus_set() call, you'll see the selection.

</F>

Perfect! Thanks Fredrik.

Strange behaviour though (IMHO), that the selection is only shown if
the widget has focus. I just tried adding another component to the
test, and switching from widget to widget does indeed stop the
selection showing!

Cheers,
 
G

greg

Ant said:
Strange behaviour though (IMHO), that the selection is only shown if
the widget has focus.

It's only strange if you're used to certain platforms.
This is normal behaviour in the Macintosh world. One
of the original Apple UI Guidelines was that there
should only be one selection visible at a time, so
that it's always clear what you're operating on.

(They seem to be breaking that now in MacOSX, which
I think is a backward step...)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top