You obviously didn't understand my first point. You need to use more frames. Frames are like invisible boundaries which are packed just like any other object. You have 3 menu choices. I would make 3 frames, one for each choice. Each choice has 3 objects, a label, entry window, button. To display a frame, pack it into Mainwindow or perhaps another frame. To remove a frame from view, unpack it. I don't have time to write the code right now, but that should work. At any time, you will have zero or one of the 3 "choice frames" packed and view-able.
To clear the entry window, set the -textvariable variable to a null string.
Try the above and see what happens.
Update: I used the wrong terminology, instead of "unpack()", use "packForget()". My idea was correct, but name was wrong. Use "packForget" to remove an object from being displayed. I did a quick hack to my previously posted code, only for the "reset password" menu. See Below. Click on Menu->password reset option, gives display of the reset objects, enter a user, hit ok on the confirmation pop-up, that will print to stdout and also clear the 3 reset objects in the window. Going through Menu->password reset option again will repeat this behavior. Note the reset to the $user variable.
Homework for the OP: Make more Frames. Probably a MenuFrame and at perhaps even a "work frame" packed into the MainWindow. You can make a frame for each of the menu options. Then pack() or packForget() those frames into either the "work frame" or the Mainwindow to show or not show them. In my hack I just directly packForget() each of the individual widgets, don't do that. Use a frame to group them together. My code is just "proof of concept", not finished code. There are hide and un-hide options for entire windows.
#!/usr/local/bin/perl
use strict;
use warnings;
use Tk;
my $mainwindow = MainWindow->new();
$mainwindow->geometry("600x150");
$mainwindow->title("Add/Delete/Reset Password");
# Disable the window Resize
# $mainwindow->resizable(0,0); # Why??? don't need it?
my $main_menu = $mainwindow->Menu();
$mainwindow->configure(-menu => $main_menu);
# File
my $menu_menu = $main_menu->cascade(-label=>"Menu", -underline => 0, -
+tearoff=>0);
$menu_menu->command(-label=>"Password Reset for users", -underline=
+>0, -command=>\&Show_ResetPassword_menu);
$menu_menu->command(-label=>"Add User", -underline=>0, -command=>\&
+Show_AddUser_menu);
$menu_menu->command(-label=>"Delete User", -underline=>0, -command=
+>\&Show_DeleteUser_menu);
# About
$main_menu->command(-label=>"About",
-command=>sub{$mainwindow->messageBox(-title=> "Ab
+out", -message=>"Version 3.0.0", -type => "ok")});
my $label_resetp;
my $entry_resetp;
my $button_resetp;
MainLoop();
my $user; #text vasriable
sub password_reset
{
my $answer = $mainwindow -> messageBox(
-message=>"Password Reset for user: $user",
-title=> "Password",
-type=>"YesNoCancel",
-icon=>"info");
if ($answer =~ /yes/i)
{
# code to reset password goes here....
# whatever log messasges you want
print "PASSWORD RESET SUCEEDED: $user\n"; # a log entry?
}
else
{
print "PASSWORD RESET ABORTED: $user\n"; # a log entry?
}
$user=''; # clear the user name entry field
$label_resetp->packForget();
$entry_resetp->packForget();
$button_resetp->packForget();
}
sub Show_ResetPassword_menu
{
$label_resetp = $mainwindow->Label(-text=>"Password Reset for user
+ ", -width => 40) -> pack ( -side=>'left');
$entry_resetp = $mainwindow->Entry(-width => 30, -textvariable =>\
+$user )->pack(-side => 'left');
$entry_resetp->bind("<Return>", \&password_reset);
$button_resetp = $mainwindow->Button(-text=>"Click to reset", -com
+mand => \&password_reset , -width => 15) -> pack ( -side=>'bottom' );
}
sub add_user
{
my $answer = $mainwindow -> messageBox(
-message=>"Add user: $user",
-title=> "Password",
-type=>"ok",
-icon=>"info");
}
sub Show_AddUser_menu
{
my $label_adduser = $mainwindow->Label(-text=>"Add user ", -width
+=> 40) -> pack ( -side=>'left');
my $entry_adduser = $mainwindow->Entry(-width => 30)->pack(-side =
+> 'left');
# $entry_adduser->bind("<Return>", \$adduser_handle_return);
my $button = $mainwindow->Button(-text=>"Click to reset", -command
+ => \&adduser, -width => 15) -> pack ( -side=>'bottom' );
}
sub Show_DeleteUser_menu {
my $deleteuser_handle_return;
my $label_deleteuser = $mainwindow->Label(-text=>"Delete User ", -widt
+h => 40) -> pack ( -side=>'left');
my $entry_deleteuser = $mainwindow->Entry(-width => 30)->pack(-side =>
+ 'left');
$entry_deleteuser ->bind("<Return>", \$deleteuser_handle_return);
my $button = $mainwindow->Button(-text=>"Click to reset", -command =>
+\$deleteuser_handle_return, -width => 15) -> pack ( -side=>'bottom' )
+;
$deleteuser_handle_return = sub {
$mainwindow -> messageBox(
-message=>"Delete User",
-title=> "DELETE",
-type=>"ok",
-icon=>"info");
}
}
| [reply] [d/l] |
Dear Marshall,
Thank you so much for sharing your code and it was great. and its working now compare to my code So when I type something and hit enter or press reset it will clear the current form to blank window and I can select again new options without any issue
my new assignement now is I'm looking to enhance the TK gui form script that I created. Because if you run the script and I click the 'File->Add user' then suddenly for example I change my mind to click 'File-Reset user' instead. The label description will display on top of each other.
| [reply] |
Dear fsmendoza,
Glad to hear that you are making progress! You didn't post your code, but I have a good guess as to what is happening...
My code was a demo, the main purpose of which, was to show you how to "packForget()" and demo what that does. Hide and unhide are for whole windows; pack and packForget are more appropriate for modifying a window that remains on the screen. I put the packForget() code into the subroutine password_reset(). You shouldn't put it there. I did it that way as just a quick demo "hack".
Normally you want the selected option, like password_reset to remain visible to user until he selects another option. Not have it "go away" at the confirmation window and force the user to re-select that option again from the File menu. Maybe the user wants to reset the passwords on multiple accounts? Or perhaps the user wants to work with a window to create multiple accounts?
Re-iterating a few points: You should group the objects for each option into a frame. You will wind up with 3 frames, add, delete, reset. pack and unpack those frames as per the user selection. You can have some variable like "$currently_displayed_option" which has either "nothing" (undef) or a reference to the currently_displayed frame.
When the user selects an option, you want to packForget the currently displayed option (if any). Then display the "new option" that the user selected (of course updating the "$currently_displayed_option").
You are getting "option widget on top of option widget" because the rule that only one or zero options should be displayed at a time is being violated.
Hope this helps.
| [reply] |
| [reply] [d/l] |