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

perl TK: Dynamically changing number of inputs at run time.

by Ganesh Bharadwaj1 (Sexton)
on Jun 17, 2016 at 06:09 UTC ( #1165938=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks, I have currently developed a perl file to convert Celsius to Fahrenheit. The code looks like this

#!/usr/local/bin/perl use Tk; my $mw = new MainWindow; # Main Window my $VARIABLE; 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) -> + 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 { $txt->delete("1.0", 'end'); my $celsius = $ent -> get(); my $farienhiet = ($celsius*9/5)+32; $txt -> insert('end', "Farienhiet is $farienhiet degrees."); }

I want the user to enter the number of values for which he wants to do Celsius to Fahrenheit. Assume he wants to convert 1 celcius value to fareinhiet, my code works. If he wants to convert 4 or 5 or 6, my code will not be able to do that. I want to let the user decide the number of values of celcius he wants to enter. Is this possible.

I was thinking of implementing a scale, like

my $scl = $mw -> Scale(-label=>"No of Celcius Values to convert :", -orient=>'v', -digit=>1, -from=>1, -to=>10, -variable=>\$celcius, -tickinterval=>2);

where the user can select any number of values from 1-10. If he selects 1, the grid can have one text location to enter and one text output, if he selects 2, then the grid will have 2 locations to enter text and 2 locations to show the fareinheit value and so on. Is this possible? Your help is greatly appreciated.

Replies are listed 'Best First'.
Re: perl TK: Dynamically changing number of inputs at run time.
by graff (Chancellor) on Jun 17, 2016 at 18:54 UTC
    To expand a bit on the first part of the first reply, you can define your "push_button" sub like this:
    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); }
    That way, it shows both the input and output values, and doesn't erase any previous information. (But since the information is in an editable Text widget, the user can easily delete lines.)
Re: perl TK: Dynamically changing number of inputs at run time.
by Anonymous Monk on Jun 17, 2016 at 07:51 UTC

    If he wants to convert 4 or 5 or 6, my code will not be able to do that.

    :) it already does do that, the user just has to change the number, your program only has to stop clearing the output window

    Is this possible?

    Sure, but does it make a good interface?

    See these examples of this type of thing (as I don't feel like writing any code at the moment) Re: Tk: Creating label in hash, Re^5: Multiple frames, the "secret" is understanding scoping and life cycle of variables

      Hi Monk, Thank you for your help. Yes. I do understand that. But that is not the problem I am facing. What I actually expect is that, the user initially specifies a number, say 3. Which means he wants to enter 3 Celsius values simultaneously in 3 different boxes. So dynamically, I can get 3 boxes are created, so that I can enter the Celsius values in 3 different boxes. Also there are 3 different boxes at the text location with 3 different values of Fahrenheit. Assuming user selects that he wants to enter 4 celcius values simultaneously, 4 enter boxes and 4 results boxes are created and so on.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1165938]
Approved by kevbot
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2021-04-11 06:29 GMT
Find Nodes?
    Voting Booth?

    No recent polls found