Selection in Tkinter Text widget.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ant

    #1

    Selection in Tkinter Text widget.

    Hi all,

    I have been trying to select text in a Text widget programmaticall y. 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(SE L, "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...

  • Fredrik Lundh

    #2
    Re: Selection in Tkinter Text widget.

    "Ant" <antroy@gmail.c om> wrote:
    [color=blue]
    > I have been trying to select text in a Text widget programmaticall y. 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(SE L, "1.0", END)[/color]

    area.focus_set( )
    [color=blue]
    > 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...[/color]

    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>



    Comment

    • Ant

      #3
      Re: Selection in Tkinter Text widget.


      Fredrik Lundh wrote:
      ....[color=blue]
      > 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>[/color]

      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...

      Comment

      • greg

        #4
        Re: Selection in Tkinter Text widget.

        Ant wrote:
        [color=blue]
        > Strange behaviour though (IMHO), that the selection is only shown if
        > the widget has focus.[/color]

        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

        Comment

        Working...