Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Disabling the keyboard inputs in Entry/Text widget [Tk GUI Code]

by kcott (Archbishop)
on Aug 26, 2022 at 14:27 UTC ( [id://11146454]=note: print w/replies, xml ) Need Help??


in reply to Disabling the keyboard inputs in Entry/Text widget

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

Replies are listed 'Best First'.
Re^2: Disabling the keyboard inputs in Entry/Text widget [Tk GUI Code]
by KishKishore (Novice) on Aug 29, 2022 at 04:56 UTC

    Hi Ken,

    This works great. Thanks for the reply :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2024-03-29 09:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found