Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

A Tk::DialogBox Tk::Entry question.

by Anonymous Monk
on Oct 13, 2014 at 06:10 UTC ( [id://1103589]=perlquestion: print w/replies, xml ) Need Help??

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


Hello Monks,

I'm not sure what exactly is causing the problem. There aren't any past threads I could see on this topic which mention this issue either. Anyway, below is a small program which consistently demonstrates this problem.

This program when run brings up a window with 'OK' button which when clicked brings a dialog box. This dialog box has an Entry widget which displays a text: HAPPY 07-OCT-2014 and also the default buttons (OK and Cancel)

Once the dialog box pops up, here are the steps to re-produce this problem

  1. highlight a portion of the text displayed in the entry widget (i.e. HAPPY 07-OCT-2014 ) by draging the mouse

  2. Press 'Backspace' key on the keyboard so that the highlighted portion of the text is deleted.

  3. Now press 'OK' any number of times, the dialog box stays there and dialog_callback is not called.
    (NOTE: after pressing the backspace but before clicking OK button, make sure not to switch the focus from this dialog box to other windows or applications)

  4. Only when you move the mouse pointer even little outside the OK button and then you bring it back into OK button, does make the OK button work as desired.

  5. The same sequence can be tried with 'Cancel' button as well.

use warnings; use strict; use Tk; my $mw = MainWindow->new; $mw->Button (-text=>'OK', -command=>\&popup_dialog)->pack; MainLoop; sub popup_dialog { my $dialog = $mw->DialogBox( -title=>'A Tk DIALOG BOX', -buttons=>['OK', 'Cancel'], -default_button=>'OK', -cancel_button=>'Cancel', -command=>\&dialog_callback); my $entry = $dialog->Entry( -text=>'HAPPY 07-OCT-2014', -borderwidth=>3, -takefocus=>1, -font=>'{Architects Daughter} -15 bold') ->grid(-padx=>"1m", -pady=>"2m", -sticky=>'w'); #workaround code -- begin $entry->bind('<Key-BackSpace>'=>sub{ print "inside BackSpace callback\n"; $_[0]->icursor(0); $_[0]->eventGenerate('<ButtonPress-1>'); $_[0]->eventGenerate('<ButtonRelease-1>'); $_[0]->eventGenerate('<ButtonPress-1>'); $_[0]->eventGenerate('<ButtonRelease-1>'); $_[0]->selectionClear; $_[0]->focus;}); #workaround code -- end $dialog->Show; } sub dialog_callback { print "inside dialog callback\n"; }

Please note that there is a backspace binding for the entry widget. This was done only to workaround another problem. i.e. if the binding was not there, then it causes a frequent but sporadic problem. Here are the steps to re-produce that problem
  1. remove the backspace binding and run the program.

  2. Now highlight a portion of the text other than the date. ie. (do not highlight 07-OCT-2014 but only the text HAPPY and the blank space after it) by dragging the mouse from right to left and the hit backspace key. (this should be done failrly quickly i.e. dragging the mouse right to left to highlight the text and hitting backspace)
  3. Now you would see that for some reason the zero in remaining text after deletion (i.e. 0 in 07-OCT-2014) is highlighted and clicking the OK button doesn't work.

But applying this work around for this frequent-but-sporadic problem brought in the consistent problem stated above.

Couple of weeks ago I filed query on 'Table Matrix row selection question' A TableMatrix row selection question and the monks indeed came to my help with detailed answers. I sincerely hope there would be few monks to help this time as well.

Lastly here are the system details I'm using:
System Configuration: I'm using Windows XP, Strawberry Perl v5.16.3, Tk::DialogBox.pm v4.016, Tk::Entry.pm v4.018 (from strawberry/perl/site/lib/Tk)

Many Thanks

Replies are listed 'Best First'.
Re: A Tk::DialogBox Tk::Entry question. (workarounds are bad)
by Anonymous Monk on Oct 13, 2014 at 08:21 UTC
      Try  $dialog->Show('global');
        Thank you very much for responding to my question. After seeing your reply I quickly checked at the docs for 'global'.

        As per perldoc for Tk::Entry it's about the grab thing (i.e. grabbbing the input -- mouse and keyboard). Also from the book 'Mastering Perl/Tk by Oreily Press Chapter 13 - a global grab means no applications in the entire system can get input except the one window that has done the global grab. But Windox XP does allow me to select through mouse as well type into other applications in the system. I do not know if that's how global grab is implemented in this OS.

        Anyway, back to the original question, your fix works correctly when the workaround code is removed. Therefore out of curiosity and for learning purposes my question is
        1. In the first place, how's is the fix for this problem related to grab thing?
        2. When the workaround code is in place this fix does not work. Therefore, is the grab and virtual event somehow opposed to each other ?
        3. Also as an aside, I created a binding for the paste (i.e. Control-v) as there is no default binding for it,
          $entry->bind('<Control-Key-v>'=>sub{ $_[0]->delete ('sel.first', 'sel.last'); $_[0]->insert ('insert', .....); });
          but how can I get the string to be pasted passed into the callback(hence left out as five dots above) ?
          But when I comment out the line calling insert method, the paste works as expected. That said, as per the perldoc for Tk::Entry there is no default binding for Control-v. So how does this work then. I'm confused.
        I'm really curious. So please let me know.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-24 00:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found