Widget referencing problem 
Author Message
 Widget referencing problem

Hello,
in an application I open toplevel windows by pressing respective buttons
and keep its "topic" in a hash table %tlwins declared in the main
program; e.g. I have a button "Foo" which opens a toplevel window for
stuff with that topic, and "Foo" has the number 2 in my topic list. In
the sub, I have
    my $tpwin=$main->Toplevel(-title=>$wtitle);
    $tlwins{$tpwin}=$opt;
$opt is the topic number, i.e. 2 in my example. OK, so long it works:
e.g. $tpwin is
Tk::Toplevel=HASH(0x200b1ef8).
Now I want to avoid to raise two toplevel windows for the same topic. If
I press the button "Foo" again, I don't want it to open another toplevel
window, but to raise the existing one. I tried
#       see if toplevel window is already there
        foreach (keys %tlwins) {
            if (defined $tlwins{$_} && $tlwins{$_} == $opt) {
                print "found $_ $tlwins{$_}\n";
                $_->deiconify();
                $_->raise();
                return;
            }
        }
The print line is for debugging, and it shows me that in the second call
the previously stored values of $tpwin and $opt are still correctly
stored. Nevertheless, the sub fails with this error:
Tk::Error: Can't locate object method "deiconify" via package
"Tk::Toplevel=HASH
(0x200b1ef8)" at texdoctk line 182.
 [\&main::tpslct,0,[svjog,svjour special: Journal of Geodesy
(jog),latex/geophys
ics/journals/jogguide.dvi
]]
 Tk callback for .frame1.button
 Tk::__ANON__ at /usr/local/lib/perl5/site_perl/5.005/aix/Tk.pm line 229
 Tk::Button::butUp at
/usr/local/lib/perl5/site_perl/5.005/aix/Tk/Button.pm line
 110
 (command bound to event)
I took the above code more or less literally out of a book, so I really
don't know why it fails, because the values actually are there and they
are correct. Can anybody give me a hint?
Thanks in advance,
--
------------------------------------------------------------------------
Thomas Ruedas
Institute of Meteorology and Geophysics, J.W.Goethe University Frankfurt

http://www.*-*-*.com/ ~ruedas/
------------------------------------------------------------------------


Fri, 06 Sep 2002 03:00:00 GMT  
 Widget referencing problem

Quote:

> Hello,
> in an application I open toplevel windows by pressing respective
buttons
> and keep its "topic" in a hash table %tlwins declared in the main
> program; e.g. I have a button "Foo" which opens a toplevel window for
> stuff with that topic, and "Foo" has the number 2 in my topic list. In
> the sub, I have
> my $tpwin=$main->Toplevel(-title=>$wtitle);
> $tlwins{$tpwin}=$opt;
> $opt is the topic number, i.e. 2 in my example. OK, so long it works:
> e.g. $tpwin is
> Tk::Toplevel=HASH(0x200b1ef8).
> Now I want to avoid to raise two toplevel windows for the same topic.
If
> I press the button "Foo" again, I don't want it to open another
toplevel
> window, but to raise the existing one. I tried
> # see if toplevel window is already there
> foreach (keys %tlwins) {
> if (defined $tlwins{$_} && $tlwins{$_} == $opt) {
> print "found $_ $tlwins{$_}\n";
> $_->deiconify();
> $_->raise();
> return;
> }
> }

The problem, Thomas, is that the key stored in %tlwins is the
stringified reference to the object, not its actual reference - i.e.
the key is the string "Tk::Toplevel=HASH(0x200b1ef8)" (without the
quotes, of course).
This is the way Perl stores references as keys (I'd be a meanie and
point you at a FAQ, but don't know the right one!).
What you need to do is store the real reference, too, something like:
$tlwins{$tpwin}{opt} = $opt;
$tlwins{$tpwin}{nRef} = $tpwin;
to store the toplevel details, and
$_{nRef}->deiconify();
$_{nRef}->raise();
to retrieve and display it.
(Apologies for syntax errors etc in the above, but it's late...!)
Hope this points you in the right direction
James

Sent via Deja.com http://www.deja.com/
Before you buy.



Fri, 06 Sep 2002 03:00:00 GMT  
 Widget referencing problem

Quote:
>(I'd be a meanie and point you at a FAQ, but don't know the right

one!).

Oops, what I meant, of course was "ONLY point you at a FAQ"!

James

Sent via Deja.com http://www.deja.com/
Before you buy.



Fri, 06 Sep 2002 03:00:00 GMT  
 Widget referencing problem
Steve Lidie kindly pointed out that I could use the hash the other way
round and use Exist, deiconify and raise then. It works now.
--
------------------------------------------------------------------------
Thomas Ruedas
Institute of Meteorology and Geophysics, J.W.Goethe University Frankfurt

http://www.geophysik.uni-frankfurt.de/~ruedas/
------------------------------------------------------------------------


Sat, 07 Sep 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Problem referencing widgets in subroutines

2. Problem de-referencing a reference to a typeglob stored in a hash

3. how to use symbolic reference widgets names

4. Reference to calling widget

5. Reference to a widget without using a global variable

6. OO trouble with bind - lost reference to widget!

7. Composite Widget with NO widgets - Possible?

8. Can Tk Widget and Xt Widget work together?

9. Is there a Tree widget in perlTk that resembles the Tree widget in Tk

10. Graph widgets in general (was Re: BLT Graph Widget)

11. pTk 8.4.24 widget patch #1 - widget demo and friends bug fixes/modernizations

12. Dealing with a reference to a hash of references

 

 
Powered by phpBB® Forum Software