By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,281 Members | 2,216 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,281 IT Pros & Developers. It's quick & easy.

Selection in Tkinter Text widget.

P: n/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,

--
Ant...

Jun 2 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Ant" <an****@gmail.com> wrote:
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>

Jun 2 '06 #2

P: n/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,

--
Ant...

Jun 2 '06 #3

P: n/a
Ant wrote:
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...)

--
Greg
Jun 3 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.