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

Hi, I have an Entry/Text widget. I have some buttons with some letter. I want user to click on only those buttons and enter in Entry/Text widget and not from computer keyboard. Is there a way where i can disable computer keyboard inputs from entering into Entry/Text widget? I found below code to disable all bindings. But with this both keyboard and mouse are getting disabled. I want mouse to act as normal.

$entry->bindtags(undef);

Replies are listed 'Best First'.
Re: Disabling the keyboard inputs in Entry/Text widget [Tk GUI Code]
by kcott (Archbishop) on Aug 26, 2022 at 14:27 UTC

    On rereading your post and responses, I think it probably is Tk::Entry that you're talking about.

    There's a few tricky bits and gotchas when setting up -state => 'readonly'; and also when button presses result in writing to a readonly widget. So, here's some code to get you started.

    #!/usr/bin/env perl use strict; use warnings; use Tk; my $mw = MainWindow::->new(-title => "Ken's Guess"); my %frame_pack_opts = qw{-fill x -padx 10 -pady 10}; my $app_preamble_F = $mw->Frame()->pack(%frame_pack_opts); my $letter_buttons_F = $mw->Frame()->pack(%frame_pack_opts); my $copy_from_F = $mw->Frame()->pack(%frame_pack_opts); my $paste_to_F = $mw->Frame()->pack(%frame_pack_opts); my $exit_F = $mw->Frame()->pack(%frame_pack_opts); chomp(my $app_preamble = <<'EOT'); The "Copy from:" entry can only be modified with the "Select:" buttons. Any text here may be selected and copied. The "Paste to:" entry can be modified in any way you want. It's primary function was to test that text copied from the "Copy from:" entry could be pasted; however, you can also type text from the keyboard, select existing text, and delete text. EOT $app_preamble_F->Label(-text => $app_preamble, -justify => 'left' )->pack(-side => 'left'); my $copy_from_entry; my $copy_from_value = ''; $letter_buttons_F->Label(-text => 'Select: ')->pack(-side => 'left'); for my $letter ('A' .. 'G') { $letter_buttons_F->Button(-text => $letter, -command => sub { $copy_from_entry->configure(-state => 'normal'); $copy_from_value = $letter; $copy_from_entry->configure(-state => 'readonly'); } )->pack(-side => 'left'); } $copy_from_F->Label(-text => 'Copy from: ')->pack(-side => 'left'); $copy_from_entry = $copy_from_F->Entry( -textvariable => \$copy_from_value, -state => 'readonly' )->pack(-side => 'left'); chomp(my $paste_to_preamble = <<'EOT'); I have to assume that the widget, where you want to paste this text, is part of another application. Otherwise, this makes no sense at all. EOT $paste_to_F->Label(-text => $paste_to_preamble, -justify => 'left' )->pack(-anchor => 'w', -pady => 5); $paste_to_F->Label(-text => 'Paste to: ')->pack(-side => 'left'); $paste_to_F->Entry()->pack(-side => 'left'); $exit_F->Button(-text => 'Exit', -command => sub { exit; })->pack(); MainLoop;

    This code represents a complete, working GUI. I suggest running it and understanding how it works; then make a copy and adapt that for your needs.

    I'm still somewhat mystified with regards to what your ultimate goal might be. Have a read of "XY Problem" and ask yourself if that might apply here.

    — Ken

      Hi Ken,

      This works great. Thanks for the reply :)

Re: Disabling the keyboard inputs in Entry/Text widget
by kcott (Archbishop) on Aug 26, 2022 at 11:48 UTC

    G'day KishKishore,

    "I have an Entry/Text widget"

    You've used the term "Entry/Text widget" multiple times. There are many widgets with either "Entry" or "Text" in their names; as far as I know, there are none with both. Here's a couple of guesses.

    If it's not one of those, state precisely what you mean and provide a link so that there's no misunderstanding.

    — Ken

Re: Disabling the keyboard inputs in Entry/Text widget
by Discipulus (Canon) on Aug 26, 2022 at 07:55 UTC
    Hello KishKishore,

    if keyboard input is not allowed why to use an Entry widget? I'd rather use a Label widget for this: pressing your buttons will simply append to the variable bound to the Label

    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, Thanks for the reply.

      I want to copy that text and paste it in another widget. I can't copy the text on Label. But I can copy the text on Entry/Text widget.

        Maybe something like:

        use strict; use warnings; use Tk; my $MW = tkinit; my $show_button_text = 'txt'; my $show_button = $MW->Button( -textvariable => \$show_button_text, -command => sub { print "$show_button_text \n"; }, )->pack(qw(-side top -fill x)); my %add_buttons; for my $add_text (qw(a bb xyz)) { $add_buttons{$add_text} = $MW->Button( -text => $add_text, -command => sub { $show_button_text .= " $add_text"; }, )->pack(qw(-side left -expand 1)); } MainLoop;
        Instead of printing, clicking $show_button might do the copy to the other widget. Tested under Strawberry Perl 5.8.9.5 (32-bit). (Update: Also works under Strawberry 5.30.3.1 64-bit.)


        Give a man a fish:  <%-{-{-{-<