Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Change the Behaviour of a Button in Tk::Dialog

by choroba (Cardinal)
on May 15, 2017 at 14:32 UTC ( [id://1190309]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks, especially the followers of the Tk cult. Is there a more elegant way how to change the behaviour of a button in a Tk dialog? Using Walk works correctly, but the code is long and unreadable. Having a more straightforward way of identifying the button object would be great (ideally something like $dialog->get_button_by_text('OK');).

#! /usr/bin/perl use warnings; use strict; use Tk; sub show_dialog { my ($mw) = @_; my $dialog = $mw->Dialog(-buttons => ['OK', 'Cancel']); my $lb = $dialog->Listbox( -listvariable => my $valid = [ 'valid', 'invalid' ] )->pack; $dialog->Walk(sub { my $widget = shift; if ('Button' eq $widget->class && 'OK' eq $widget->cget('-text +')) { my $orig = $widget->cget('-command'); $widget->configure(-command => sub { if ('valid' eq $valid->[ $lb->curselection->[0] ]) { $orig->[0]->(); } else { $dialog->Dialog(-title => 'Invalid', -text => 'Invalid', -bitmap => 'error', -buttons => [ 'OK' ] )->Show; } }); } }); $dialog->Show; } my $mw = 'MainWindow'->new(-title => 'Validation'); $mw->Button(-text => 'Show Dialog', -command => [\&show_dialog, $mw])->pack; $mw->Button(-text => 'Quit', -command => \&Tk::exit)->pack; MainLoop();

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re: Change the Behaviour of a Button in Tk::Dialog
by zentara (Archbishop) on May 15, 2017 at 16:32 UTC
    Hi choroba,

    ideally something like $dialog->get_button_by_text('OK');).

    The perldoc fro Tk::Dialog says it has the same advertised widgets as Tk::DialogBox, which is shows the buttons available as B_"button-text". So in your case it would be

    my $button_ok = $dialog->{SubWidget}{B_OK};

    I'm not really a human, but I play one on earth. ..... an animated JAPH
      Hi ++zentara, as always you got the simplest and clearest solution, but:

      > The perldoc fro Tk::Dialog says it has the same advertised widgets as Tk::DialogBox

      I cannot find this explicitally said, I only see Tk::DialogBox in the see also section.

      When i read the doc of Tk::Dialog i've not understood what advertised was supposed to mean. It is a general Tk concept?

      L*

      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
        Hi Discipulus, right in the link you gave to Tk::Dialog it says:

        ADVERTISED WIDGETS
        Because Tk::Dialog is a subclass of Tk::DialogBox it inherits all the advertised subwidgets of its superclass: e.g. "B_button-text", where 'button-text' is a Button's -text value. Additionally, Tk::Dialog advertises:

        Yes, advertised widgets are a general feature of Tk, and most compound widgets will have them. See perldoc Tk::mega . All subclasses of mega widgets inherit the parent widgets advertised widgets.


        I'm not really a human, but I play one on earth. ..... an animated JAPH
      Thanks, that's exactly what I needed. Somehow, I overlooked the part in Tk::DialBox . The code is now shorter by 7 lines!
      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Change the Behaviour of a Button in Tk::Dialog
by thanos1983 (Parson) on May 15, 2017 at 14:51 UTC

    Hello choroba,

    I have never used Tk, but I found Tk::Button. Is this something that you are looking for?

    You can find an example here: Masterin Perl/Tk (Chapter 2. Geometry Management).

    Update: Or maybe (perl/tk changing button commands).

    Update2: From the documentation of (Tk::Dialog):

    When it's time to use a dialog, invoke the Show method on a dialog object; the method then displays the dialog, waits for a button to be invoked, and returns the text label of the selected button..

    Most likely this could the work for you.

    Hope this helps.

    Seeking for Perl wisdom...on the process of learning...not there...yet!

    16 May 2017 Athanasius Fixed link to Mastering Perl/Tk

      I know Tk::Button . The problem is that adding Buttons to a Dialog via $dialog->Button(-text=>'Press me!')->pack; adds them to a different part of the dialog window than the buttons listed in the constructor. I need access to the latter ones.

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2024-04-18 13:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found