Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

How do I determine if a Tk window exists?

by FireBird34 (Pilgrim)
on Jan 02, 2004 at 05:14 UTC ( [id://318242]=perlquestion: print w/replies, xml ) Need Help??

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

I'm coding an application that requires a status bar. After some problems, I was able to get that working, however I again run into a new problem (suprise suprise! lol). I have the status bar in a new Tk window for convienence sake, however while the status bar is doing it's update, and the window is shut down, I get a nice error. ie:

#some code here
$status_bar->update;
#more code here

#user action: shut down $status_bar

error:
Tk::Error Usage $widget->update(...)
And so forth

Point being, how do I test to see if $status_bar actually exists before the update code is run? This way, I could at least counter it in some way, without having to deal with nasty error messages?
  • Comment on How do I determine if a Tk window exists?

Replies are listed 'Best First'.
Re: How do I determine if a Tk window exists?
by Coruscate (Sexton) on Jan 02, 2004 at 05:52 UTC

    use Win32::API; Win32::API->Import('user32', 'FindWindowA', 'PP', 'N'); my $title = 'Title/Window Caption of StatusBar Window'; if (FindWindowA(0, $title)) { # window exists, do status bar update }

    Of course, that is Win32-specific. I couldn't help posting that. I've been fiddling with Win32's API lately and find it quite interesting (if at times frustrating because of Win32::API::Struct and other things). I haven't used Tk as of yet, so I can't be certain of this, but can you not keep a $status_open variable and hook into the event of a closed window and set the variable appropriately. Thus, before you update the status, check the variable to ensure the window is open. Doesn't stop the user from closing the window right in the middle of your update though. Why not create a status window the user cannot close (surely Tk allows you to create windows without minimize, maximize, closing functionality).

Re: How do I determine if a Tk window exists?
by mark4 (Acolyte) on Feb 22, 2017 at 14:31 UTC
    I didn't like waitWindow() because it will stop all program execution after that point. The code below works and seems to be a better solution (and is non-OS Specific) (Note, toplevel window(s) do continue after waitWindow() and can call and run subroutines(callbacks), In the case I needed to get around I needed code to run after the waitWindow() )
    use Tk; use strict; my $tl; #<- Make this avaliable for testing globally. my $mw = MainWindow->new; $mw->title("MainWindow"); $mw->Button(-text => "Toplevel", -command => \&do_Toplevel)->pack( ); $mw->Button(-text => "test Toplevel", -command => \&test_toplevel)->pa +ck( ); MainLoop; sub do_Toplevel { if (! Exists($tl)) { $tl = $mw->Toplevel( ); $tl->title("Toplevel"); $tl->Button(-text => "Close", -command => sub { $tl->withdraw })->pack; } else { $tl->deiconify( ); $tl->raise( ); } } sub test_toplevel { if ( Exists($tl)) { print "Top level is here\n"; } else { print "Top level is gone\n"; } }
Re: How do I determine if a Tk window exists?
by zentara (Archbishop) on Jan 02, 2004 at 18:23 UTC
    On linux, I'm thinking you could make your status_bar a toplevel widget, and you could do something like this snippet
    #!/usr/bin/perl use Tk; $mw = MainWindow->new; $mw->title("MainWindow"); $mw->Button(-text => "Toplevel", -command => \&do_Toplevel)->pack( ); MainLoop; sub do_Toplevel { if (! Exists($tl)) { $tl = $mw->Toplevel( ); $tl->title("Toplevel"); $tl->Button(-text => "Close", -command => sub { $tl->withdraw })->pack; } else { $tl->deiconify( ); $tl->raise( ); } }

    Here is an example which signals the MainWindow if a toplevel window is closed. I'm sure you can find a way to work it in to your script.

    #!/usr/bin/perl -w # waitwindow.pl use Tk; use strict; my $mw = MainWindow->new(); my $toplevel_count = 0; my $last_window_died = 1; $mw->Checkbutton( -text => 'No toplevel windows alive', -variable => \$last_window_died, )->pack(); $mw->Button( -text => 'Create a toplevel', -command => sub { my $toplevel = $mw->Toplevel; $toplevel_count++; $last_window_died = 0; $toplevel->Button( -text => 'Destroy Me', -command => sub {$toplevel->destroy} )->pack(); $toplevel->waitWindow; #main window keeps processing events... #this code is executed after $toplevel is destroyed $toplevel_count--; if ($toplevel_count == 0) { #toggle the checkbutton $last_window_died = 1; } },#end sub )->pack( -fill => 'x', -expand => 1, ); $mw->Button( -text => 'Quit', -command => sub {$mw->destroy}, )->pack( -fill => 'x', -expand => 1, ); MainLoop();

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://318242]
Approved by Popcorn Dave
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-25 10:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found