Tkinter and Text() widget interactivity ?

T

Tonino

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
 
P

Pierre Quentel

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
 

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,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top