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

I want to use PerlTray to add a perltray icon to my perl application im compiling with perltray using PDK I have looked at the sample code that activestate gives but nothing is making sense. I also want to add right click and and exit functionality to my current code below.

Also was curious if the icon size had to be 16x16 in order for an icon to display in the systray?
#!perl while (1) { use strict; use warnings; use LWP::UserAgent; use Time::Piece; use Win32; use PerlTray; my $t = localtime; my $username = Win32::LoginName; my $output_file = "C:/Users/$username/Dropbox/My_IP.txt"; my $ua = new LWP::UserAgent; $ua->agent('Mozilla/5.0'); my $response = $ua->get("http://automation.whatismyip.com/n09230945.as +p"); my $content = $response->content; open(my $output_fh, '>>', $output_file) or die "You may not have dropbox installed failed to open - $outpu +t_file $!"; print {$output_fh} "Your IP address is $content at $t\n"; close $output_fh; sleep 1800; }

Replies are listed 'Best First'.
Re: PerlTray question and adding right click exit option on tray icon
by cavac (Prior) on Jul 02, 2012 at 15:53 UTC

    Here's an example. This shows me the current space weather as tray icon. Right click displays details (as menu entries), clicking on any of those reloads the data.

    #!/usr/bin/perl -w use strict; use warnings; use strict; use warnings; use PerlTray; use Ham::Reference::Solar; my $solarstate; my $errorstate; my $lastkindex = -1; SetIcon("nostate"); my %icons = ( -2 => "errorstate", -1 => "nostate", 0 => 'k0', 1 => 'k0', 2 => 'k0', 3 => 'k0', 4 => 'k0', 5 => 'k1', 6 => 'k2', 7 => 'k3', 8 => 'k4', 9 => 'k5', ); sub internalUpdate { undef $solarstate; my $solar = new Ham::Reference::Solar; if($solar->is_error) { undef $solarstate; $errorstate = $solar->error_message; SetIcon("errorstate"); Balloon("Failed to update solar weather status", "Solar Weathe +r", "error"); $lastkindex = -2; } else { $solarstate = $solar->get_hashref; my $newk = $solarstate->{'k-index'}; if($newk != $lastkindex) { if($lastkindex >= 0 && $newk > $lastkindex) { Balloon("Geomagnetic activity has risen!", "Solar Weat +her", "warning"); } my $oldicon = $icons{$lastkindex}; my $newicon = $icons{$newk}; SetIcon($newicon); SetAnimation("0:05", 200, ($oldicon, $newicon)); $lastkindex = $newk; } } SetTimer("10:00"); } sub PopupMenu { my($default,@menu) = "*"; if(defined($solarstate)) { foreach my $key (sort keys %$solarstate) { push @menu, ["$key: " . $solarstate->{$key}, \&Timer]; } } else { push @menu, ["ERROR: $errorstate", \&Timer]; } return [ @menu, ["-------"], ["Refresh", \&Timer], ["Exit", "exit"] ]; } sub Timer { internalUpdate() } sub ToolTip {"Solar Weather Tray"} Timer();

    Sorry for any bad spelling, broken formatting and missing code examples. During a slight disagreement with my bicycle (which i lost), i broke my left forearm near the elbow. I'm doing the best i can here...

        That should be the Popup sub. Can't actually run and test it at home cause i messed up my windows partition...

        Sorry for any bad spelling, broken formatting and missing code examples. During a slight disagreement with my bicycle (which i lost), i broke my left forearm near the elbow. I'm doing the best i can here...
      Thanks for the example but I am still not sure how to incorporate this to my current code yes I would like the systray icon to display and exit option from the menu
Re: PerlTray question and adding right click exit option on tray icon
by zentara (Cardinal) on Jul 02, 2012 at 15:39 UTC
    I looked at PerlTray's doc's on ActiveState and it looks to me like they are using the Perl packer Par or pp, just from looking at the option list. They give mouse options for click and double-click and that is it. I have a feeling you would have a hard time hacking PerlTray. :-)

    If you want a REAL configurable TrayIcon see Gtk2::StatusIcon Demo, it has right-click bindings.


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      Getting GTK to work in Windows is quite a Pain in the seating subsystem.

      Sorry for any bad spelling, broken formatting and missing code examples. During a slight disagreement with my bicycle (which i lost), i broke my left forearm near the elbow. I'm doing the best i can here...