Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Tk pop-up if condition is met

by IB2017 (Pilgrim)
on Aug 05, 2020 at 21:18 UTC ( [id://11120364]=perlquestion: print w/replies, xml ) Need Help??

IB2017 has asked for the wisdom of the Perl Monks concerning the following question:

Is it possible to allow a popup in a text widget only if a condition is met, i.e. if a text has been selected? This is the code I have so far, which creates a popup, however, it checks if something has been selected only agter its creation.

use strict; use warnings; use Tk; use Tk::Text; my $mw = tkinit(); my $text = $mw->Text()->pack(); $text->insert('end',"This is my first line\n"); $text->insert('end',"This is my second line\n"); PupupTextWidget($mw, $text); $mw->MainLoop(); sub PupupTextWidget{ my ($mw, $obj) = @_; my $messagePopupSave; if ($^O eq 'MSWin32'){ $messagePopupSave="Do something (ctrl+s)"; }else{ $messagePopupSave="Do something (cmd+s)"; } my $menu = $mw->Menu(-tearoff=>0, -menuitems=>[ [command=>$messagePopupSave, -command=>[sub { my $UserInput=$obj->getSelected; print $UserInput; }, $obj,]], [qw/command Copy/, -command=>['clipboardCopy', $obj,]], ]); $obj->menu($menu); return $obj; }

Replies are listed 'Best First'.
Re: Tk pop-up if condition is met
by jcb (Parson) on Aug 05, 2020 at 22:27 UTC

    You probably want to configure the -postcommand on the Menu object; it is a callback invoked just before the menu appears and can modify the menu contents.

      Thank you. But how? I am trying several things with no success. For example

      my $menu = $mw->Menu(-tearoff=>0, -postcommand => sub { return if $o +bj->getSelected eq "" }, -menuitems=>[ [command=>$messagePopupSave, -command=>[sub { my $UserInput=$obj->getSelected; print $UserInput; }, $obj,]], [qw/command Copy/, -command=>['clipboardCopy', $obj,]], ]);

        In the case where I needed this, I was attaching a submenu for suggestions while adding live spelling check to a Tk::Text widget. The code is basically a first draft of the concept as part of one of my personal "toy" projects. Here are some pieces:

        my $main_text_field = $top->Text(-width => 132, height => 24, -setgrid => 1, -wrap => 'word'); my $main_text_field_spelling_menu = $main_text_field->menu->Menu(-tear +off => 0); $main_text_field_spelling_menu->configure (-postcommand => [\&post_suggestion_menu, $main_text_field, $main_text_field_spelling_menu]) +; $main_text_field->menu->insert ('View', cascade => -label => 'Respell...', -underline => 0, -menu => $main_text_field_spelling_menu);
        sub post_suggestion_menu { my $widget = shift; my $menu = shift; my @wordpos; $menu->delete(0, 'end'); # extract word boundaries where user requested menu into @wordpos if (exists $spelling_suggestions{$word}) { $menu->add(command => -label => $_, -command => [\&replace_word, $widget, @wordpos, $_]) foreach @{$spelling_suggestions{$word}}; } else { $menu->add(command => -label => 'No suggestions.', -state => 'disa +bled'); } }

        The -postcommand callback is able to edit the menu before it is displayed, although in my use, I wanted a fully dynamic menu that is rebuilt every time the user (me) asks for it. Note that the menu will be displayed (as far as I know) after -postcommand returns. If you want to prevent displaying the menu at all, you would need to replace the binding that posts the menu.

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11120364]
Approved by davies
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-25 04:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found