Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with Tk

Status
Not open for further replies.

GeniuS22

MIS
May 25, 2002
29
CA
# this is an example script

#!perl -W

use strict;
use Tk;

my $safe=0;

sub newm {
if ($safe==0) {
$safe=1;
my $newm=new MainWindow;
my $but=$newm->Button(
-command=>sub{$safe=0;$newm->destroy;},
-text=>'OK2',
)->pack;
}
}

my $main=new MainWindow;

my $but1=$main->Button(
-command=>\&newm,
-text=>'OK',
)->pack;

MainLoop;

# the $safe is to make sure the 2nd window does not laod more then one at a time.
# The script loads a window with a button. Upon pressing the 'OK' button is loads another window. when you click on the 'OK2' button it destroys the 2nd window and goes back to the previous one. When pressing on the X on the 2nd window, and try to reload it, it doesn't do anything.
How can i get rid of the minimize,maximize,close buttons on a window or how to bypass this situation. Knowledge is the first step to Greatness!!!
 
Hi there, look at this line from your code:

-command=>sub{$safe=0;$newm->destroy;},

- take the $safe=0 out of that line and put it after so the code looks like this...

sub newm {
if ($safe==0) {
$safe=1;
my $newm=new MainWindow;
my $but=$newm->Button(
-command=>sub{$newm->destroy;},
-text=>'OK2',
)->pack;
$safe=0;
}
}


and you should find that it works ok...

As for disabling the actual windows controls - not sure that it's possible.

Let me know if that helps...

ice

 
No it does not work
I dont think you understand what i want
the $safe is to block the loading of the 2nd windows more then once. With your suggestion, if you press ok 5 times, 5 windows will load. The way i did it works fine execpt when you press the X button on the second window and try to reload the 2nd window,from the first window, the 2nd window does not load!

Anyone???
Knowledge is the first step to Greatness!!!
 
Ok - get your point - why don't you get rid of the $safe variable then and use "Exists" to check it the the window widget has already been created:

if (Exists($widget)) {

blah blah blah

}

Give it a try...

ice
 
NO it does not work because i declare the variable in the sub and once you leave to sub the $var is destroyed. Also when trying to load the 2nd window, it cannot detect it because it does not exists yet!!

Thanks but i need this to work ASAP!!

If you can think of anuthing else Knowledge is the first step to Greatness!!!
 
#!perl -W
# try this one
use strict;
use Tk;
my $newm;
my $main=new MainWindow;
my $but1=$main->Button(
-command=> \&newm,
-text=>'OK',
)->pack;

MainLoop;

sub newm {
if (Exists($newm)) {$newm->destroy;}
$newm=new MainWindow;
my $but=$newm->Button(
-command=>sub{$newm->destroy;},
-text=>'OK2',
)->pack;
} ---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
Here's one way to do it. This method disables the button when it is clicked. It then enables the button, if the 2nd window is closed.

use strict;
use Tk;
Code:
use vars qw($but1); # Global variable

sub newm {
   $but1->configure(-state, 'disabled');
   my $newm=new MainWindow;
   $newm->protocol('WM_DELETE_WINDOW' => sub {$but1->configure(-state,'normal'); $newm->destroy;});
   my $but=$newm->Button(
       -command=>sub{$but1->configure(-state,'normal'); $newm->destroy;},
       -text=>'OK2',
   )->pack;
}

my $main=new MainWindow;
$but1=$main->Button(
    -command=>\&newm,
    -text=>'OK',
)->pack;

MainLoop;
 
so raider2001 if i understand
$newm->protocol('WM_DELETE_WINDOW' ...
is like a 'on exit' command
sweet thanks for that tip ---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
Thanks raider

Can you disble the $main windows instead of the button
Because if you exit the first window, the 2nd one freezes. Knowledge is the first step to Greatness!!!
 
Added code to destroy $newm if $main is closed.
Code:
use strict;
use Tk;

use vars qw($but1 $newm); # Global variables

sub newm {
   $but1->configure(-state, 'disabled');
   $newm=new MainWindow;
   $newm->protocol('WM_DELETE_WINDOW' => sub {$but1->configure(-state,'normal'); $newm->destroy;});
   my $but=$newm->Button(
       -command=>sub{$but1->configure(-state,'normal'); $newm->destroy;},
       -text=>'OK2',
   )->pack;
}

my $main=new MainWindow;
$main->protocol('WM_DELETE_WINDOW' => sub {$newm->destroy; $main->destroy});
$but1=$main->Button(
    -command=>\&newm,
    -text=>'OK',
)->pack;

MainLoop;
 
Thanks raider

It works fine.
Knowledge is the first step to Greatness!!!
 
another question raider
can we use standar winAPI message with protocol ?
like example
WM_RESIZE
WM_KEYUP
WM_MOUSEMOVE
etc...
what are the limitation ? (since its based on c not c++)
thanks

---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
Not sure about other winAPI messages with protocol. The documentation for Tk-Wm only mentions the following window manager protocols:

WM_DELETE_WINDOW
WM_SAVE_YOURSELF
WM_TAKE_FOCUS

The other options is to use Tk-Bind.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top