开发者

Perl/Tk : Getting the selected value of option menu

I am de开发者_JAVA百科veloping one interface in Perl/Tk.

In that I am using one optionmenu to list the names of the users.

And while selecting the user from optionmenu it should display corresponding date of birth of the employee.

And I should be able to update the date of birth of the selected user.

I have written the following code.

$dob_label = $form_name -> Label(-text=>"BirthDay")->place(-x=>150,-y=>200);

$dob=$form_name->DateEntry(-width=>11,-parsecmd=>\&parse,-formatcmd=>\&format)->place(-x=>250,-y=>200);

$ename = $form_name->Optionmenu(-variable=>\$select_value,-options => [@names],
-command=>sub {&get_id_date($hash_ref,$eid,$dob,$_[-1])})->place(-x=>250, -y=>100);

$post_button=$form_name->Button(-text=>"Add",-command=>[\&Add_Birthday,$select_value,$dob,"edit"])->place(-x=>250,-y=>275);

The function get_id_date is used to get the id and dob of employee using the name of the employee.

It is returning the correct id and dob.

Then I edited the dob of the employee.

And I am calling Add_Birthday function to save the changes into database. But what is the problem in this is,the variable $select_value is always having the value of first name in the optionmenu. Actually it should have the value of the last selected item in the optionmenu.

So what is the problem in this code,

Please give the solution for this also.

Thanks in advance.


When you create the Button, you are passing the current value of $select_value to the button/command set up. By the time you press the button, the old value of $select_value has already been evaluated and set in the command argument list. You need to make your command a closure, so that $select_value is not evaluated until the button is pressed, e.g.:

-command => sub { Add_Birthday($select_value, "dob", $edit) }

For completeness, I should also mention that another way of doing this is to pass in references:

-command => [\&Add_Birthday, \$select_value, "dob", \$edit]

But that requires rewriting the function to accommodate the references in the argument list.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜