Tkinter and Text() widget interactivity ?
quentel.pierre at wanadoo.fr
Tue Mar 1 21:32:08 CET 2005
Tonino a écrit :
> 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
> 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
Here is an example of what you can do :
# from Tkinter import *
# def action(event):
# new_window = Toplevel(root)
# n_text = Text(new_window)
# 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")
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"
More information about the Python-list