Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

How to change colour of a Tkx button when pressed

by PerlCowboy (Novice)
on Jun 24, 2016 at 14:32 UTC ( [id://1166504]=perlquestion: print w/replies, xml ) Need Help??

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

Hey there PerlMonks! Almost got it, but one thing I'm having issues with is I am trying to change the color of a button once it is pressed. I currently have a series of 18 buttons using the Tkx module and am wondering how I could change the color of a button once it is pressed. I know it's on the command line, but can you have two commands on a button? My code is listed below.

use strict; use warnings; use Tkx; open(INFILE,"d1528235.txt") or die "could not open file for reading!\n +"; open(OUTFILE, '>forecast.txt') or die "Cannot open forecast.txt: $!"; while(<INFILE>){ if (m/^\s*$/) { next; } chomp $_; my @fields = split(/\ /,$_); my @output; foreach my $field(@fields){ if($field =~ /^\*?[ABMQRWY][A-Z0-9]{4}235 / ){ push @output,$field; } # print OUTFILE "$_\n"; } if (@output) { my $line = join('',@output); print "$line\n"; print OUTFILE "$line\n"; } } my $mw = Tkx::widget->new("."); $mw->g_wm_title("Jobs Not Forecasted"); $mw->g_wm_minsize(200, 200); Tkx::button(".b", -text => "18-Letters", -width => 11, -command => sub { print OUTFILE "18L\n"; -background =>'red'}, ); Tkx::pack(".b"); Tkx::button(".c", -text => "07-Orders", -width => 11, -command => sub { print OUTFILE "07O\n";}, ); Tkx::pack(".c"); Tkx::button(".d", -text => "36-Letters", -width => 11, -command => sub { print OUTFILE "36L\n";}, ); Tkx::pack(".d"); Tkx::button(".e", -text => "38-Letters", -width => 11, -command => sub { print OUTFILE "38L\n";}, ); Tkx::pack(".e"); Tkx::button(".f", -text => "11-Orders", -width => 11, -command => sub { print OUTFILE "11O\n";}, ); Tkx::pack(".f"); Tkx::button(".g", -text => "99-Letters", -width => 11, -command => sub { print OUTFILE "99L\n";}, ); Tkx::pack(".g"); Tkx::button(".h", -text => "21-Letters", -width => 11, -command => sub { print OUTFILE "21L\n";}, ); Tkx::pack(".h"); Tkx::button(".i", -text => "23-Letters", -width => 11, -command => sub { print OUTFILE "23L\n";}, ); Tkx::pack(".i"); Tkx::button(".j", -text => "Return to L&I", -width => 11, -command => sub { print OUTFILE "OOO\n";}, ); Tkx::pack(".j"); + Tkx::button(".k", -text => "EOSSS235", -width => 11, -command => sub { print OUTFILE "EOSSS235\n";}, ); Tkx::pack(".k"); Tkx::button(".l", -text => "M4020235", -width => 11, -command => sub { print OUTFILE "M4020235 20\n";}, ); Tkx::pack(".l"); Tkx::button(".m", -text => "PRTAR235", -width => 11, -command => sub { print OUTFILE "PRTAR235\n";}, ); Tkx::pack(".m"); Tkx::button(".n", -text => "WR115235", -width => 11, -command => sub { print OUTFILE "WR115235\nWR415235\n";}, ); Tkx::pack(".n"); Tkx::button(".o", -text => "X01RA235", -width => 11, -command => sub { print OUTFILE "X01RA235\n";}, ); Tkx::pack(".o"); Tkx::button(".p", -text => "X1514235", -width => 11, -command => sub { print OUTFILE "X1514235 14\n";}, ); Tkx::pack(".p"); Tkx::button(".q", -text => "X4020235", -width => 11, -command => sub { print OUTFILE "X4020235\n";}, ); Tkx::pack(".q"); + Tkx::button(".r", -text => "SICLIC", -width => 11, -command => sub { print OUTFILE "SICLIC\n";}, ); Tkx::pack(".r"); Tkx::button(".s", -text => "EXIT", -width => 11, -command => sub { Tkx::destroy("."); }, ); Tkx::pack(".s"); Tkx::MainLoop(); exec `crossref.pl`;

Replies are listed 'Best First'.
Re: How to change colour of a Tkx button when pressed
by PerlCowboy (Novice) on Jun 24, 2016 at 16:39 UTC

    I FIGURED IT OUT!!!!! The following is the line of code on the second line of the -command line. You just have to change the letter to match the button.

    Tkx::i::call(".b", "configure", "-background", "blue");
      You could have also killed the button on press, and then made a new one in its place... the color you wanted.

      This is most curious since all the docs I have read says Tkx buttons don't have any color attributes. NTL, I tried for some time to make your code work with the "eureka I found it pseudo-code" and failed. Would you have time to reply again with an exact replacement for the bad code in the OP? As in, ...broke code line... ... working code line... Would be greatly appreciated. BTW: I am using W7/64, /w perl 5, version 24, subversion 0 (v5.24.0) built for MSWin32-x64-multi-thread ..blah.. Binary build 2400 300558 provided by ActiveState ...Built Jun 9 2016 21:44:59 it that helps. Much thanks.

        This is most curious since all the docs I have read says Tkx buttons don't have any color attributes.

        background is http://www.tcl.tk/man/tcl8.4/TkCmd/options.htm#M-background

        NTL, I tried for some time to make your code work with the "eureka I found it pseudo-code" and failed.

        floor slippery?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-04-23 12:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found