Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Using Win32::GUI in a package

by Nitrox (Chaplain)
on Dec 15, 2002 at 15:51 UTC ( #220011=perlquestion: print w/replies, xml ) Need Help??

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

I'm sure I'm overlooking something obvious but I've banged my head on the keyboard for long enough. Below I've included a small test case to demonstrate my dilemma.

Its a simple script that displays a dialog window which contains a single button. Clicking the button or the 'close app' icon (small 'X' on the title bar) should end the program run.

It works as expected until I uncommment the package declaration line. At that point the button clicks are no longer recognized and using the 'close app' icon only hide()'s the window and doesn't properly call the *_Terminate function.

Is there something I'm doing wrong or is it an issue with Win32::GUI accessing package level items even though the session was initiated from the same package? -Nitrox

#package gui_test; use strict; use warnings; use Win32::GUI; display_gui(); exit; sub btnCancel_Click {-1;} sub Main_Terminate {-1;} sub display_gui { my $main = Win32::GUI::DialogBox ->new( -name => "Main", -text => "GUI Test", -width => 200, -height => 200, -top => 200, -left => 200, ); my $btn_cancel = $main->AddButton( -name => "btnCancel", -cancel => 1, -text => "Cancel", -left => 100, -top => 100, ); $main->Show(); Win32::GUI::Dialog(); }

Replies are listed 'Best First'.
Re: Using Win32::GUI in a package
by jplindstrom (Monsignor) on Dec 15, 2002 at 19:02 UTC
    When you uncomment the line, you move your event handler subs from package main to package gui_test. But Win32::GUI doesn't know that.

    To get Win32::GUI to call the event handler for the btnCancel_Click in the new package, I think you need to set the -name of the button to "gui_test::btnCancel".

    That seems very impractical, so I never do that in my programs. I use modules and stuff though, but I always place my event handlers in package main, like this

    sub ::btnCancel_Click {

    I don't know if that's the right way, or the best way, but it works for me.


      I don't know if that's the right way, or the best way, but it works for me

      And it works for me too, thanks much for a solution jplindstrom!


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2023-05-30 18:16 GMT
Find Nodes?
    Voting Booth?

    No recent polls found