I'm working on a msn style "toast" alert in perl/tk. While I'm still early on in development, I've hit a bit of a snag. I'm not too familiar with how perl handes object insances, but I seem to be having a problem where my objects seem to be interfering with one another.
Example, when clicking on the "Toast" button on the main window, the toast window appears and begins it's routine. When the ".toast" window is selected on the Toast window, the window shrinks like it's supposed to. However, if I open another toast window while there is already one displayed, the second window will shrink like it's supposed to, but the first window will not and it looks like the geometry values it's getting are from the second window, not the first. Create a couple of toast windows, wait for them to load, then try to close them all to see what I mean. How do I make it so variables are unique to each instance of a Toast object?
Toast.pm
package Tk::Toast;
use vars qw($VERSION);
$VERSION = '0.1';
use strict;
use Carp;
use Tk::StayOnTop;
use Tk::After;
use base qw(Tk::Toplevel);
Construct Tk::Widget 'Toast';
sub Populate {
my ($self, $args) = @_;
$self->update;
$self->overrideredirect(1)
;
my $widgetwidth = $self->height;
my $widgetheight = $self->width;
my $widgetx = $self->toplevel->screenwid
th - $widgetwidth;
my $widgety = $self->toplevel->screenhei
ght;
$self->stayOnTop;
$self->geometry("$widgetwi
dth" . 'x' . "$widgetheight" . '+' . "$widgetx" . '+' . "$widgety");
}
sub Grow {
my ($self, $args) = @_;
my $grow;
$grow = $self->repeat(10, sub{ Grow_backend($self, $grow);});
}
sub Grow_backend {
my ($self, $timer) = @_;
my @growy = split(/\+/, $self->geometry);
my $growy = $growy[2];
$growy--;
my $growwidgetwidth = $self->height;
my $growwidgetheight = $self->width;
my $growwidgetx = $self->toplevel->screenwid
th - $growwidgetwidth;
my $growwidgety = $self->toplevel->screenhei
ght;
if($growy < ($growwidgety - $growwidgetheight)) {
$timer->cancel;
}
$self->geometry("$growwidg
etwidth" . 'x' . "$growwidgetheight" . '+' . "$growwidgetx" . '+' . "$growy");
print $self->geometry . "\n";
}
sub Shrink {
my ($self, $args) = @_;
my $shrink;
$shrink = $self->repeat(10, sub{ Shrink_backend($self, $shrink);});
}
sub Shrink_backend {
my ($self, $timer) = @_;
my @shrinky = split(/\+/, $self->geometry);
my $shrinky = $shrinky[2];
$shrinky++;
my $shrinkwidgetwidth = $self->height;
my $shrinkwidgetheight = $self->width;
my $shrinkwidgetx = $self->toplevel->screenwid
th - $shrinkwidgetwidth;
my $shrinkwidgety = $self->toplevel->screenhei
ght;
if($shrinky > $shrinkwidgety) {
$timer->cancel;
}
$self->geometry("$shrinkwi
dgetwidth"
. 'x' . "$shrinkwidgetheight" . '+' . "$shrinkwidgetx" . '+' . "$shrinky");
print $self->geometry . "\n";
}
1;
Test.pl
use lib 'c:/perl/';
use Tk;
use Toast;
$main = new MainWindow();
$button = $main->Button(-text => 'Toast!', -command => \&toaster)->pack();
MainLoop;
sub toaster() {
$toast = $main->Toast();
$button2 = $toast->Button(-text => $toast, -command => sub{$toast->Shrink;})->pac
k();
$toast->Grow();
}
Start Free Trial