0

I am using a Text widget.

I have over-ridden the right click to display a popup menu in my Perl/Tk GUI. But whenever I right click at any position, the text from the earlier cursor location till the location where I have right clicked gets highlighted.

I don't know what is causing this, so I simply want to programmatically deselect this highlighted text.

How do I go about doing this?

Thanks!

EDIT:

I have made a bind for right-click and this is the subroutine that is called:

sub rightClickMenu {
    my ($self, $x, $y) = @_;
    $txt->tagRemove('sel', '1.0', 'end');
    $rightMenu -> post($x, $y);
    $txt->tagRemove('sel', '1.0', 'end');
}

I have removed the sel tag twice (just to be sure). $rightMenu is the menu that is popped up. It shows perfectly fine when right-clicked.

Liam Willis
  • 661
  • 6
  • 17

1 Answers1

1

The selection in the text widget is handled by setting the tag sel for the selected range of characters. This tag can be removed like this:

.t tag remove sel 1.0 end

assuming the pathname of your text widget is .t. This specifies that for all characters from the first (1.0) to the character position after the last character (end) the tag sel is to be removed.

Note: normally when removing a tag one has to deal with the possibility that it has been assigned to multiple ranges in the text. The tag removal invocation above clears the tag from the whole text, and that's fine for the selection tag since you're (usually) only supposed to have one selected range anyway. If there are multiple ranges that have the tag foo and you want to clear just one of them, you first need to find the starting and ending indices of that range and clear (by calling tag remove) the tag only between those.

Note 2: All this is assuming that the visible effect is actually caused by the sel tag getting set. In Tk, it's not a standard binding for button 2 to set this tag: it could be that some non-standard binding in Perl-Tk sets some other tag that is displayed visually in the same way as the sel tag is. For further investigation, this command may be useful:

.t tag names $placeWhereIRightClicked

(again assuming the pathname of your text widget is .t, and that placeWhereIRightClicked holds the index of the place where the right clicking occurred) will tell you all tags that are active at that index.

(The command

.t tag names

will list tags for the whole text.)

TkDocs has an article about the text widget where the tag remove command is mentioned, but how to do it in Perl-Tk isn't showed.

The CPAN documentation for the text widget says that the syntax for the command is

$text->tagRemove(tagName, index1, ?index2, index1, index2, ...?)

so I suppose

$text->tagRemove('sel', '1.0', 'end')

or something like that is the way to do it (no Perl, can't test).

(Note: the 'Hoodiecrow' mentioned in the comments is me, I used that nick earlier.)

Peter Lewerin
  • 13,140
  • 1
  • 24
  • 27
  • Sorry, I don't think I can help you then. It's likely to be a Perl-Tk issue, since it works just fine with Tcl/Tk. (You _did_ substitute your own widget-holding variable for `$text`, etc, right?) – Peter Lewerin Jan 25 '14 at 19:17
  • Oh! It seems the indices, even the quasi-numerical, must be strings. Try specifying '1.0' instead. – Peter Lewerin Jan 25 '14 at 19:20
  • Yes I did change it. Still nothing. The text gets selected till the position where I right click. – Liam Willis Jan 25 '14 at 19:26
  • Even after changing the index? According to the Perl-Tk docs, this is the way to do it, so something must be set up the wrong way. Could there possibly be another tag (besides `'sel'`) that gets set and which is displayed the same way as the selection by your text widget? – Peter Lewerin Jan 25 '14 at 19:29
  • Yes, even after changing that. No, since I haven't used **tags** at all (other than what you have written above), that possibility can be ruled out. – Liam Willis Jan 25 '14 at 19:46
  • Just to be sure: 1) you right-click, 2) the text gets selected, 3) your code removes the `'sel'` tag, 4) the text still stays selected? I.e. the events happen in that order? – Peter Lewerin Jan 25 '14 at 21:24
  • Yes. Exactly in that order! – Liam Willis Jan 26 '14 at 14:34