Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Tkinter and Text() widget interactivity ?

Reply
Thread Tools

Tkinter and Text() widget interactivity ?

 
 
Tonino
Guest
Posts: n/a
 
      03-01-2005
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

 
Reply With Quote
 
 
 
 
Pierre Quentel
Guest
Posts: n/a
 
      03-01-2005
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
 
Reply With Quote
 
 
 
 
Tonino
Guest
Posts: n/a
 
      03-02-2005
Many thanks for this - will give it a bash

Tonino

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Tkinter.event.widget: handler gets name instead of widget. Terry Reedy Python 15 07-16-2012 10:32 AM
Re: Tkinter.event.widget: handler gets name instead of widget. Frederic Rentsch Python 0 07-09-2012 08:39 PM
Tkinter.event.widget: handler gets name instead of widget. Frederic Rentsch Python 0 07-08-2012 09:19 PM
Re: [Tkinter-discuss] Please help -- Tkinter Scale widget withDoubleVar is acting weird Jeff Epler Python 0 08-23-2004 02:31 PM
Re: [Tkinter-discuss] Please help -- Tkinter Scale widget withDoubleVar is acting weird Jeff Epler Python 0 08-20-2004 12:07 PM



Advertisments