f



Text Widget: Highlighting search terms with the text.

I'm writing a little application to search through data. I would like
to be able to go through the text displayed in the Text Widget and
highlight (change the colour or bold) the words which were used as the
search term.

In this little example, I would like to use the highlightText sub to
bold the word "line" throughout the Text Widget. Is this possible?

use Tk;

my $mw = MainWindow->new(-title => 'Hymns GUI');

my $but1 = $mw -> Button(-text => 'Insert Text',
                         -command => \&insertText)->pack(-side =>
'top', -fill => 'both', -expand => 1);

my $but2 = $mw -> Button(-text => 'Highlight Text',
                         -command => \&highlightText)->pack(-side =>
'top', -fill => 'both', -expand => 1);

my $txt = $mw->Scrolled ( 'Text',
                         -font => [-family => 'Arial Unicode MS', -
size => '9', -weight => 'normal'],
                         -width => 50,
                         -height => 10,
                         -relief => 'sunken',
                         -scrollbars => 'e',
                         -wrap => 'none' ) -> pack(-side => 'bottom', -
fill => 'both', -expand => 1);

MainLoop;

sub insertText
{
  $txt->delete("1.0", 'end');
  $txt->insert('end', "First line.\n");
  $txt->insert('end', "Second line.\n");
}

sub highlightText
{
  #Not sure what to do here.
}

Thanks,
Dave
0
david
1/12/2008 6:01:19 PM
comp.lang.perl.tk 4721 articles. 0 followers. pharrendorf (19) is leader. Post Follow

3 Replies
1322 Views

Similar Articles

[PageSpeed] 44

david schrieb:
> I'm writing a little application to search through data. I would like
> to be able to go through the text displayed in the Text Widget and
> highlight (change the colour or bold) the words which were used as the
> search term.
> 
> In this little example, I would like to use the highlightText sub to
> bold the word "line" throughout the Text Widget. Is this possible?
> 
> use Tk;
> 
> my $mw = MainWindow->new(-title => 'Hymns GUI');
> 
> my $but1 = $mw -> Button(-text => 'Insert Text',
>                          -command => \&insertText)->pack(-side =>
> 'top', -fill => 'both', -expand => 1);
> 
> my $but2 = $mw -> Button(-text => 'Highlight Text',
>                          -command => \&highlightText)->pack(-side =>
> 'top', -fill => 'both', -expand => 1);
> 
> my $txt = $mw->Scrolled ( 'Text',
>                          -font => [-family => 'Arial Unicode MS', -
> size => '9', -weight => 'normal'],
>                          -width => 50,
>                          -height => 10,
>                          -relief => 'sunken',
>                          -scrollbars => 'e',
>                          -wrap => 'none' ) -> pack(-side => 'bottom', -
> fill => 'both', -expand => 1);
> 
> MainLoop;
> 
> sub insertText
> {
>   $txt->delete("1.0", 'end');
>   $txt->insert('end', "First line.\n");
>   $txt->insert('end', "Second line.\n");
> }
> 
> sub highlightText
> {
>   #Not sure what to do here.
> }
> 
> Thanks,
> Dave

Hi,

maybe you are looking for the 'FindAll' Method?

'perldoc Tk::Text'

    *$text*->FindAll(*mode, case, pattern*)
        Removes any current selections and then performs a global text
        search. All matches are tagged with the sel tag.

        *mode* can be be -exact or -regexp. See the search command for more
        information

        *case* can be -nocase or -case. See the search command for more
        information

        *pattern* is an exact string to match if *mode* is -exact or a
        regular expression if the match *mode* is -regexp.



Christoph


-- 
use Tk;use Tk::GraphItems;$c=tkinit->Canvas->pack;push@i,Tk::GraphItems->
TextBox(text=>$_,canvas=>$c,x=>$x+=70,y=>100)for(Just=>another=>Perl=>Hacker);
Tk::GraphItems->Connector(source=>$i[$_],target=>$i[$_+1])for(0..2);
$c->repeat(30,sub{$_->move(0,4*cos($d+=3.16))for(@i)});MainLoop
0
Ch
1/12/2008 10:08:57 PM
On Sat, 12 Jan 2008 10:01:19 -0800 (PST), david <davidhooey@gmail.com>
wrote:

>I'm writing a little application to search through data. I would like
>to be able to go through the text displayed in the Text Widget and
>highlight (change the colour or bold) the words which were used as the
>search term.
>
>In this little example, I would like to use the highlightText sub to
>bold the word "line" throughout the Text Widget. Is this possible?

Hi, see http://perlmonks.org?node_id=351865

I took the sub from the widget demo, IIRC.

zentara

-- 
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
0
zentara
1/13/2008 3:08:04 PM
On Jan 13, 10:08 am, zentara <zent...@highstream.net> wrote:
> On Sat, 12 Jan 2008 10:01:19 -0800 (PST), david <davidho...@gmail.com>
> wrote:
>
> >I'm writing a little application to search through data. I would like
> >to be able to go through the text displayed in the Text Widget and
> >highlight (change the colour or bold) the words which were used as the
> >search term.
>
> >In this little example, I would like to use the highlightText sub to
> >bold the word "line" throughout the Text Widget. Is this possible?
>
> Hi, seehttp://perlmonks.org?node_id=351865
>
> I took the sub from the widget demo, IIRC.
>
> zentara
>
> --
> I'm not really a human, but I play one on earth.http://zentara.net/japh.html

Thanks to all especially zentara for your example code. For reference
this is the updated example...

use Tk;

my $mw = MainWindow->new(-title => 'Highlighting');

my $but1 = $mw -> Button(-text => 'Insert Text',
                         -command => \&insertText)->pack(-side =>
'top', -fill => 'both', -expand => 1);

my $but2 = $mw -> Button(-text => 'Highlight Text',
                         -command => \&highlightText)->pack(-side =>
'top', -fill => 'both', -expand => 1);

my $txt = $mw->Scrolled ( 'Text',
                         -font => [-family => 'Arial Unicode MS', -
size => '9', -weight => 'normal'],
                         -width => 50,
                         -height => 10,
                         -relief => 'sunken',
                         -scrollbars => 'e',
                         -wrap => 'none' ) -> pack(-side => 'bottom', -
fill => 'both', -expand => 1);

MainLoop;

sub insertText
{
  $txt->delete("1.0", 'end');
  $txt->insert('end', "First line.\n");
  $txt->insert('end', "Second Line.\n");
}

sub highlightText
{
  my $string = "line";
  my $current = '1.0';
  my $length = 0;
  my $current_last;
  my $length_last;

  $txt->tagConfigure( 'search', -background => 'lightgreen', -font =>
[-family => 'Arial Unicode MS', -size => '9', -weight => 'bold'] );
  $txt->see("1.0");
  $txt->tagRemove( 'search', qw/0.0 end/ );

  while (1)
  {
    $current = $txt->search(-count => \$length, "-exact",'-nocase',
$string, $current, 'end' );
    last if not $current;
    #warn "Posn=$current count=$length\n",
    $txt->see($current);
    $txt->tagAdd( 'search', $current, "$current + $length char" );
    $current = $txt->index("$current + $length char");
  }
}

0
david
1/14/2008 5:01:39 PM
Reply:

Similar Artilces:

highlight text in text widget
Hi, I am trying to make widget that displays text. I also want a search keywords available. The way I have envisioned is something similar to emacs search, i.e. it highlights all the occurrences of the keyword that you are interested in. I am trying to find out if that kind of highlighting be done in widget_text or some other widgets. Can anyone help? Thanks neon writes: > I am trying to make widget that displays text. I also want a search > keywords available. The way I have envisioned is something similar to > emacs search, i.e. it highlights all the occurrences of the keyword &g...

text in a widget, text-widget
hello i have an application with a couple of text widgets but i would like to add some functionalities that at first and second sight are not directly supported by the plain text widget itself - ? for example i would like certain lines of text within the textwidget to take different background colors according to certain parameters also i would like to be able to detect certain sentences or words that would become 'links' and by clicking them i would open up different windows any suggestions to solutions and widgets for the above problems ? - i probably could...

Tk text search and elided text
All, I'm having trouble understanding the following text widget behavior. I'm using Tcl/Tk 8.4.9. I've got a text widget with some elided text. A search returns the position L.C (properly), but a subsequent search starting at L.C for the same string fails to return L.C. However, a search with the option -elide does return L.C. The elided text is at the beginning of the line, a good 60 characters before the text in question. Here's the example: # -------------------------------- end toplevel .bfw pack [set t [text .bfw.t]] -expand 1 -fill both set elidetext "to elid...

RE: text in a widget, text-widget
You can use tags with in the text widget. See the documentation in Tk::Text for more explanation on how they work. How to configure: $scrolled->tagConfigure('BLUE',-background => 'blue'); $scrolled->tagConfigure('RED',-foreground => 'red'); $scrolled->tagConfigure('GREEN',-foreground => 'green'); Here is how I use them: $scrolled->insert('end',$job_header,'RED') if ($job_header); $scrolled->insert('end',$running_proc,'BLUE') if ($running_proc); $scro...

Re: text in a widget, text-widget @ 1146167556
Intrah onat Diria .. Tue, 25 Apr 2006 13:55:44 -0700 ,Van Nieuwenhuize, Mike wrote "Noalim y": > > Johan, > > Here this will give you an idea how to create a link in a text widget as > well as some other tags. > > HTH > > Mike > > use strict; > use Tk; > use Tk::Dialog; > > my $mainWindow = MainWindow->new(-title => "Test Tags"); > > my $sc_width = 100; > my $sc_height = 15; > > my $scrolled = $mainWindow->Scrolled( > ...

RE: text in a widget, text-widget #2
Johan, Here this will give you an idea how to create a link in a text widget as well as some other tags. HTH Mike use strict; use Tk; use Tk::Dialog; my $mainWindow = MainWindow->new(-title => "Test Tags"); my $sc_width = 100; my $sc_height = 15; my $scrolled = $mainWindow->Scrolled( "Text", -scrollbars => 'se', -background => 'white', -wrap => &...

Re: text in a widget, text-widget @ 1145998849
Intrah onat Diria .. Tue, 25 Apr 2006 13:55:44 -0700 , ****** wrote "Revera y": > of "unsubscribe ptk" to majordomo@lists.stanford.edu > If you wish to unsubscribe from this mailing list, send the message body > This message was posted through the Stanford campus mailing list server. > ++**=3D=3D > -++**=3D=3D--++**=3D=3D--++**=3D=3D--++**=3D=3D--++**=3D=3D--++**=3D=3D--= > > > > > ,=20 > , _________________ > , |---| > , | > , lead > , ::::::opthe > , oo,loent,uaha > , }, > ,...

text-text
Wondering how what I input to my UTF-8 terminal gets passed along through my patched [1] trn ... Cyrillic: А Б В Г Д Е Ж З И Й К Л М Н О П а б в г д е ж з и й к л м н о п IPA: ᴀ ᴁ ᴂ ᴃ ᴄ ᴅ ᴆ ᴇ ᴈ ᴉ ᴊ ᴋ ᴌ ᴍ ᴎ ᴏ ɀ Ɂ ɂ Ƀ Ʉ Ʌ Ɇ ɇ Ɉ ɉ Ɋ ɋ Ɍ ɍ Ɏ ɏ [1] https://groups.google.com/d/msg/comp.sys.raspberry-pi/7Z37Hdrm0DM/6aqD-reXFzAJ ...

text + text
What is "text + text" supposed to do right now? It doesn't seem very useful to me. What about making "text + text" as an equivalent for "text || text"? Most strongly-typed programming languages do this. And MS SQL Server too, I think (CMIIW). -- dave ---------------------------(end of broadcast)--------------------------- TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org Am Freitag, 8. Oktober 2004 12:57 schrieb David Garamond: > What is "text + text" supposed to do right now? Nothing. > What a...

Slow searching in Tk text widgets under 804.
I am trying to modify some scripts I have written to work correctly under Tk 804 and have run into yet another snag that, while not horrible, is bothersome never-the-less. While doing regex searches in a text widget, searching is orders of magnitude slower under tk 804 than it was under tk 800. Searches, especially case insensitive searches, that used to take very short periods of time, well under one second, take 4, 5, up to 10 seconds under tk 804; running the same code, searching on the same string in the same text. It is not so noticable until the text gets larger than 1...

RE: text in a widget, text-widget @ 1146167556 #2
Not sure exactly what you are trying to do but it sounds like you want word that is clicked on. Quick and dirty way would be: $scrolled->selectWord(); my $word = $scrolled->getSelected(); The above will only get a single word, so would be a challenge if you have a link with multiple words. Lame way: create a separate tag for each word and bind that to a sub for that particular word. These seems like a lot of overhead and I would imagine Text widget would have better feature you can leverage on. Efficient way: ? I will leave that up to you ;) or someone else mor...

RE: text in a widget, text-widget @ 1146167556 #3
Johan, Got to thinking about something that may work for you. Please see my modified code. Basically The only thing that I give it a is a list of words, the tag and tag bind is generated on the fly. Maybe using an approach like this will get you closer to what you wanted. Still kinda lame as you have to create a new tag for each word or in this case url but it at least will allow you to do it on the fly. Mike use strict; use Tk; use Tk::Dialog; my $mainWindow = MainWindow->new(-title => "Test Tags"); my $sc_width = 100; my $sc_height = 15; ...

Search for text in a text field
Hi When posting a search box for the users, how do you take their input and search whether or not it is contained in a text field in many records and then diplay the records which contains the input. Thanks in advance, Luke. There is a function called InStr that will probably do what you require. Cheers, Chris "Luke Jones" <luke_jones_82@yahoo.co.uk> wrote in message news:24bb1508.0407080157.d587c63@posting.google.com... > Hi > > When posting a search box for the users, how do you take their input > and search whether or not it is contained in a text field in ...

Highlight text in text box
I want to highlight any existing text, in a text box, when I click on it, like when I Tab into the text box. I can't find anything in the help. Any help is appreciated ShyGuy You can use selstart and sellength properties of a textbox in order to select any existing text. In order to select the whole text of textbox01, use, me.textbox01.selstart=0 me.textbox01.sellength=len(nz(me.textbox01)) Regards, Ramesh ShyGuy <shyguy@shytown.com> wrote in message news:<44nhi0d9qnsvdru1lu5l0d3ncuvuqsi39p@4ax.com>... > I want to highlight any existing text, in a text...

Web resources about - Text Widget: Highlighting search terms with the text. - comp.lang.perl.tk

File:Map of Florida highlighting Franklin County.svg - Wikipedia, the free encyclopedia
I, the copyright holder of this work, release this work into the public domain . This applies worldwide. This file contains additional information, ...

RBA's subtle highlighting of capital gains tax, not negative gearing, over housing
There's a subtle difference between the RBA's submission to a government housing inquiry and the opening statement by its head of financial stability, ...

Apple shares new “If it’s not an iPhone, it’s not an iPhone” ad highlighting Apple Pay
... AAPL Company continue reading at 9to5Mac . What do you think? Discuss "Apple shares new “If it’s not an iPhone, it’s not an iPhone” ad highlighting ...

Highlighting The Scope Of Legal Issues That Can Arise In A Media Company’s Digital Operations
What legal issues should a broadcaster be concerned about when expanding its use of digital media?

Spotify Launches ‘Year in Music’ Highlighting 2015 Listening Habits
... In addition, the site allows users to create a Year in Music card for their stats, which can be shared on social media. Outside of highlighting ...

Highlighting the Best Rookie vs. Vet Battles in NFL Training Camps
The NFL is nothing at all like the workplace normal everyday people know, and that's a good thing. Imagine a strange world in which each spring ...

The student who lost his Facebook internship after highlighting a huge privacy flaw says it was an 'incredible ...
Harvard student Aran Khanna was due to work as an intern at Facebook this summer. However, after he highlighted a massive privacy issue in the ...

Blue-letter Bible highlighting OT passages about Jesus is top seller
A Bible with Old Testament passages relating to Jesus marked in blue is the top selling specialty Bible in North America.

Heartbreaking image of drowned toddler goes viral, highlighting severity of ...
Fox News Heartbreaking image of drowned toddler goes viral, highlighting severity of ... Fox News Sept. 2, 2015: Refugees and migrants sit ...

Highlighting notable changes for Patriots since '14 meetings with Colts
ESPN (blog) Highlighting notable changes for Patriots since '14 meetings with Colts ESPN (blog) But Arrington was released in the offseason ...

Resources last updated: 2/2/2016 2:04:09 PM