You set your interface to watch on the command line, as first argument, or it defaults to eth0. I placed it just above the lower right corner to stay out of most things way. Once the display is started, a left mouse button click on it kills it.
It also has a non-blocking sleep (thx to Slaven Reszic) that you might find useful in other Tk scripts.
#!/usr/bin/perl use warnings; use strict; use Tk; #specify interface on commandline or here my $iface = shift || 'eth0'; #correction my $mw = new MainWindow; # I have my toolbar at the top, so # I like my info boxes at the bottom $mw->geometry('-50-50'); $mw->overrideredirect(1); $mw->configure(-cursor => 'pirate'); #:-) $mw->fontCreate('big', -family=>'courier', -weight=>'bold', -size=> 18); my $bw = $mw->Label(-text=>' ', -font=>'big', -bg=>'black', -fg=>'yellow')->pack (-side=>'left',-fill =>'both'); # left click exits program $mw->bind('<1>' => sub{exit}); #refresh every 1.5 seconds my $id = Tk::After->new($mw,1500,'repeat',\&refresh); MainLoop; sub refresh{ my $r0 = `cat /sys/class/net/$iface/statistics/rx_bytes`; my $t0 = `cat /sys/class/net/$iface/statistics/tx_bytes`; tksleep($mw, 1000); my $r1 = `cat /sys/class/net/$iface/statistics/rx_bytes`; my $t1 = `cat /sys/class/net/$iface/statistics/tx_bytes`; my $rr = sprintf ("%03d",($r1 - $r0)/1024); my $tr = sprintf ("%03d",($t1 - $t0)/1024); $bw->configure(-text=>"Rx: $rr kBs || Tx: $tr kBs"); } sub tksleep { # Like sleep, but actually allows the display to be # updated, and takes milliseconds instead of seconds. # A non-blocking sleep for the eventloop my $mw = shift; my $ms = shift; my $flag = 0; $mw->after($ms, sub { $flag++ }); $mw->waitVariable(\$flag); }
In reply to Tk Bandwidth use indicator by zentara
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |