Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Funny, I was just working on ways to do these dialog tricks with Tk and Gtk2. For Tk, I came up with Tk RGBColorDialog as an example of using a toplevel as a dialog. To avoid the problem of the predefined Ok button, yet still wait for a response in a Show() method, I used a waitVariable hack in Show(). It works by having the $self->{RESULT} being set to '', and when the Ok button is pressed, it is set to $self->{value} (the return data). You probably can work out a way in Show to test the value before returning it. Like this simple way with a LABEL (there is probably a better way)
WAIT: $self->waitVariable(\$self->{RESULT}); #test result my $string = $self->{RESULT}; if( $string lt '#333333' ){ goto WAIT } else{ $self->grabRelease; $self->withdraw; &$old_focus; &$old_grab; return $self->{RESULT}; } } # end Show method
In Gtk2, the preferred method is to wrap the dialog like this. It can be done because the destroy is handled by you outside of the Dialog object. Thanks to muppet and Mathew Braid, for showing me this.
sub get_string { my ($title, $parent, $prompt, $initial_value, $default_cancel) = @_; my $dialog = Gtk2::Dialog->new ($title, $parent, [], 'gtk-cancel' => 'cancel', 'gtk-ok' => 'accept'); my $label = Gtk2::Label->new ($prompt || ''); $dialog->vbox->add ($label); $label->show (); my $entry = Gtk2::Entry->new (); $entry->set_text ($initial_value) if defined $initial_value; $dialog->vbox->add ($entry); $entry->show (); $dialog->set_default_response ($default_ok ? 'accept' : 'cancel'); my $ret; if ('accept' eq $dialog->run ()) { $ret = $entry->get_text (); } $dialog->destroy (); return $ret; }
It may be possible to subclass the Tk::Dialogbox, and override the default button presses. It has the wait Show and exit methods available, so you should be able to redefine them in a subclass, use
use base qw/Tk::Derived Tk::DialogBox/;
You can google for Tk::Derived and find examples how to do it. Basically you just make a package called MyDialog, copy the Show() sub from the Dialogbox.pm into it, and modify it to your liking. You will see where I got the idea for my code in the Dialogbox.pm. :-)

Probably the best solution, is to emulate Gtk2's wrapping of the dialog, by redefining exit() in your subclassed DialogBox, so it is controlled from outside the object.


I'm not really a human, but I play one on earth Remember How Lucky You Are

In reply to Re^2: Perl/Tk Oddities by zentara
in thread Perl/Tk Oddities by TomKane

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (1)
    As of 2018-11-14 04:33 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      My code is most likely broken because:
















      Results (163 votes). Check out past polls.

      Notices?