Tkinter and Text() widget interactivity ?

Discussion in 'Python' started by Tonino, Mar 1, 2005.

  1. Tonino

    Tonino Guest

    Hi,

    I have a small Tkinter app that gets data from a socket connection to a
    "server". The app has a Text() widget to display the info that it gets
    from the socket connection. I have the ability to stop the text at any
    point.

    What I want to be able todo is select a line from the Text() window and
    double click or whatever on it to open a new window with that selected
    text as a paramater to the new window.

    The app is a network sniffer and I want to be able to select a line
    from the Text() window and run a decode on the data from the sniffer.

    any help and pointers would help. I have no idea of what to search for
    ;)

    Thanks
     
    Tonino, Mar 1, 2005
    #1
    1. Advertisements

  2. Tonino a écrit :
    > Hi,
    >
    > I have a small Tkinter app that gets data from a socket connection to a
    > "server". The app has a Text() widget to display the info that it gets
    > from the socket connection. I have the ability to stop the text at any
    > point.
    >
    > What I want to be able todo is select a line from the Text() window and
    > double click or whatever on it to open a new window with that selected
    > text as a paramater to the new window.
    >
    > The app is a network sniffer and I want to be able to select a line
    > from the Text() window and run a decode on the data from the sniffer.
    >
    > any help and pointers would help. I have no idea of what to search for
    > ;)
    >
    > Thanks
    >


    Here is an example of what you can do :

    # from Tkinter import *
    #
    # def action(event):
    # begin,end=event.widget.tag_ranges(SEL)
    # selected_text=event.widget.get(begin,end)
    # new_window = Toplevel(root)
    # n_text = Text(new_window)
    # n_text.pack()
    # n_text.insert(END,selected_text)
    #
    # root=Tk()
    # t=Text(root)
    # t.pack()
    # t.tag_bind(SEL,"<Button-1>",action)
    # t.insert(END,"Sweetness, sweetness I was only joking when I said\n")
    # t.insert(END,"By rights you should be bludgeoned in your bed")
    #
    # root.mainloop()

    In a Text widget you can add event bindings to tags, the selected text
    has the built-in tag SEL. I don't think you can bind the event
    <Double-Button> to it because double-clicking on a text widget selects
    the word the cursor is on ; here I use <Button-1> (left click) but you
    can use other events

    In the function "action" the widget where the event occured is
    event.widget (the Text widget). Its method tag_ranges returns the
    beginning and end of the text with the specified tag (here SEL), then
    you get the selected text by the method get() of the text widget

    This is explained in "An introduction to Tkinter" by Fredrik Lundh, in
    the chapter "The Text Widget"
    http://www.pythonware.com/library/tkinter/introduction/

    Regards,
    Pierre
     
    Pierre Quentel, Mar 1, 2005
    #2
    1. Advertisements

  3. Tonino

    Tonino Guest

    Many thanks for this - will give it a bash ;)

    Tonino
     
    Tonino, Mar 2, 2005
    #3
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. T. Kaufmann
    Replies:
    0
    Views:
    468
    T. Kaufmann
    Jul 9, 2003
  2. Jane Austine

    Tkinter Text widget getting too slow

    Jane Austine, Sep 3, 2003, in forum: Python
    Replies:
    7
    Views:
    666
    Michael Peuser
    Sep 5, 2003
  3. snx7s
    Replies:
    1
    Views:
    395
    Peter Otten
    Nov 9, 2003
  4. Alex9968
    Replies:
    0
    Views:
    740
    Alex9968
    Mar 19, 2008
  5. Alex9968
    Replies:
    0
    Views:
    2,287
    Alex9968
    Mar 19, 2008
  6. dmitrey
    Replies:
    0
    Views:
    1,424
    dmitrey
    Oct 14, 2008
  7. Frederic Rentsch
    Replies:
    0
    Views:
    429
    Frederic Rentsch
    Jul 8, 2012
  8. Terry Reedy
    Replies:
    15
    Views:
    940
    Frederic Rentsch
    Jul 16, 2012
Loading...