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

Hi Monks,

I am writing a code to convert Celsius to Fahrenheit. The problem is that, I want the user to enter the number of values of celcius he wants to convert (can be implemented by a scale, range from 1 to 10). For example, if user enters 1 and pushes a button. Then the GUI will create an entry box and a text box where the output is printed. If he enters 2 and pushes the button, the GUI will create 2 entry boxes and 2 different text boxes, where the output can be printed. so in this case, if user enters 1 entry celcius value per box, in the other 2 text boxes, the individual results of farienheit will be shown. Can the GUI be dynamically changed in this manner?

I had asked this question earlier, but I think my question was not properly understandable, and though some people tried to help out, they couldn't understand the question properly.

I have also updated my code now, so that it is more clear. What I am trying to do is 2 cases. one in which the user enters 1 in the scale ($numce1), in this case, I want to create 1 entry box and one text box. If the user enters 2, I want to create 2 entry boxes and 2 text boxes which show the output. This code is not correct, I dont know what I am doing wrong. I also want to know how to expand this for 10 cases. i.e. 10 input Celsius boxes and 10 output Fahrenheit boxes

#user chooses 1 in scale. and pushes button #Enters Celcius in one entry box created. Celcius: 1 #views result in text box Farienhiet:33.8 #user chooses 2 in scale and pushes button. 2 entry boxes and 2 text b +oxes are created Enters 1 in one entry box and 0 in another entry box. Views result as 33.8 in one box and 32 in another box.

This is what I am trying to accomplish. Am I in the right direction?

#!/usr/local/bin/perl use Tk; use Tk::Pane; use Tk::Balloon; use strict; use warnings; #my $mw = new MainWindow; # Main Window our $formal_name = 'Celcius to Farienhiet Converter'; my $mw = MainWindow->new( -title => "$formal_name"); $mw->configure( -menu => my $menubar = $mw->Menu ); my $menu_help = $menubar->cascade( -label => '~Help' ); $menu_help->command( -label => "About", -command => sub { GUS::help_about::start_MainLoop() }, ); my $VARIABLE; my $VARIABLE_sec; my $numcel = 1; my $scl = $mw -> Scale(-label=>"No of Celcius Values to Enter:", -orient=>'h', -digit=>1, -from=>1, -to=>2, -variable=>\$numcel, -tickinterval=>1); my $but_sec = $mw -> Button(-text=>"Push Me", -command =>\&push_button +_sec) -> pack(); sub push_button_sec { if ($numcel ==1) { my $frm_name = $mw -> Frame() -> pack(); my $lab = $frm_name -> Label(-text=>"Enter Celsius Value:") -> pack(); my $ent = $frm_name -> Entry(-textvariable=>\$VARIABLE) -> pack(); my $but = $mw -> Button(-text=>"Push Me", -command =>\&push_button_old +) -> pack(); #Text Area my $frm_name2 = $mw -> Frame() -> pack(); my $lab2 = $frm_name2 -> Label(-text=>"Farienhiet:") -> pack(); my $txt = $mw -> Text(-width=>40, -height=>10) -> pack(); MainLoop; #This function will be executed when the button is pushed sub push_button_old { my $celsius = $ent -> get(); my $farienhiet = ($celsius*9/5)+32; my $msg = sprintf( "%s Celsius = %3.1f Fahrenheit\n", $celsius, $f +arienhiet ); $txt -> insert('end', $msg); } } if ($numcel ==2) { my $frm_name = $mw -> Frame() -> pack(); my $lab = $frm_name -> Label(-text=>"Enter Celsius Value 1:") -> pack( +); my $ent = $frm_name -> Entry(-textvariable=>\$VARIABLE) -> pack(); my $frm_name_sec = $mw -> Frame() -> pack(); my $lab_sec = $frm_name_sec -> Label(-text=>"Enter Celsius Value 1:") +-> pack(); my $ent_sec = $frm_name_sec -> Entry(-textvariable=>\$VARIABLE_sec) -> + pack(); my $but = $mw -> Button(-text=>"Push Me", -command =>\&push_button) -> + pack(); #Text Area my $frm_name2 = $mw -> Frame() -> pack(); my $lab2 = $frm_name2 -> Label(-text=>"Farienhiet1:") -> pack(); my $txt = $mw -> Text(-width=>40, -height=>2) -> pack(); my $frm_name2_sec = $mw -> Frame() -> pack(); my $lab2_sec = $frm_name2_sec -> Label(-text=>"Farienhiet2:") -> pack( +); my $txt_sec = $mw -> Text(-width=>40, -height=>2) -> pack(); MainLoop; sub push_button { my $celsius = $ent -> get(); my $farienhiet = ($celsius*9/5)+32; my $msg = sprintf( "%s Celsius = %3.1f Fahrenheit\n", $celsius, $f +arienhiet ); $txt -> insert('end', $msg); my $celsius_sec = $ent_sec -> get(); my $farienhiet_sec = ($celsius_sec*9/5)+32; my $msg_sec = sprintf( "%s Celsius = %3.1f Fahrenheit\n", $celsius +_sec, $farienhiet_sec ); $txt_sec -> insert('end', $msg); } } }

Replies are listed 'Best First'.
Re: set multiple entry boxes and text output boxes in perl TK at run time (yes)
by Anonymous Monk on Jun 20, 2016 at 07:11 UTC

    Can the GUI be dynamically changed in this manner?

    The answer is yes, it can be done like that. Its no different to do it from a slider than to do it without a slider.

    I have also updated my code now, so that it is more clear. This is what I am trying to accomplish. Am I in the right direction?

    It was always clear what you wanted to accomplish, and

    Yes, you're in the right direction in the sense that you're trying and stuff is happening, and

    Yes, you have some confusion so

    Yes, you have to start from the beginning, and I'll help you along all the way until you're finished, if you're willing to do your part and write this program with me

    Here, I'll start, your assignment is take this program(hot01.pl) and copy into hot02.pl and modify/complete how I ask, and post as a reply; it should run without warnings

    1) give "MakeStuff" and "MakeOne" names that mean something to you, your problem, your program, and

    2) Also improve MakeOne to where it packs stuff you're interested in, it should be easy

    Here it is hot01.pl

    #!/usr/bin/perl -- use strict; use warnings; use Tk qw' tkinit '; Main( @ARGV ); exit( 0 ); sub Main { my $mw = tkinit; MakeStuff( $mw, 3 ); $mw->MainLoop; } sub MakeStuff { my( $w, $howmany ) = @_; for my $n ( 1 .. $howmany ){ MakeOne( $w, $n ); } } sub MakeOne { my( $w, $text )= @_; $w->Button( -text => $text )->pack; }
Re: set multiple entry boxes and text output boxes in perl TK at run time
by Anonymous Monk on Jun 20, 2016 at 07:04 UTC

    Slightly different from yours, but it shows a way to make a variable number of entry boxes.

    #!/usr/bin/perl # http://perlmonks.org/?node_id=1166095 use strict; use warnings; use Tk; my $numcel = 1; my $mw = new MainWindow -title => 'Celcius to Farienhiet Converter'; $mw->geometry( '500x500' ); my $scl = $mw->Scale( -label=>"No of Celcius Values to Enter:", -orient => 'h', -digit => 2, -from => 1, -to => 10, -tickinterval => 1, -variable => \$numcel, )->pack( -fill => 'x' ); $mw->Button( -text=>"Push Me", -command =>\&push_button_sec, )->pack(); my $frame = $mw->Frame->pack( -fill => 'x' ); MainLoop; sub push_button_sec { $_->destroy for $frame->children; for my $i (1..$numcel) { my $answer = ''; my $input = ''; my $item = $frame->Frame()->pack(-fill => 'x'); $item->Label( -text => "Enter Celsius Value:", )->pack(-side => 'left'); my $output = $item->Entry( -textvariable => \$input, )->pack(-side => 'left'); $item->Label( -textvariable => \$answer, )->pack(-side => 'left'); $output->bind( '<Return>' => sub { $answer = sprintf '%.1f', $input * 9 / 5 + 32; }); } }