hi, today I want to explain how can you write a GUI app in Perl language. maybe after some months no writing, come back with some nice notes about perl.i’m sure that you somewhat tired of boring console apps.first of all you have to know that if ya wanna write something in Perl that is requires some extra modules, you have two choices to find & install them. one of them is PPM ( Perl package manager) that let you install packages in an easy way. another way is Cpan.it also exists in windows & Unix . with this command you can install packages: install ( package name ) .we use TK module for creating GUI forms .so lets go to the heart of matter.the main thing that you got to pay attention to it is that, its totally look like other modules in languages such as ( PHP – python ) that contain some steps like creating a new main window then place some widgets & starting off the event loop
use TK;$mh = mainWindow->new();$mh->title(“salam”);$mh->label(-text=>”just for testing”)->pack; mainloop;
let's run this program & see your first GUI application in Perl.Yes! that was so much easy to accomplish the first mission.one note is there.pack at the end of the label code is for giving a default location on the main form to it.for placing some buttons on the form we use the following example:
$mh->button(-text=>”test”)->pack;
but you might think why it doesn’t return anything to me ? yes you didn’t define any event for it.
$mh->button(-text=>”test”)->pack;mainloop; sub m2h {}print “button clicked”;
full source code to help you out and ease the process of learning
#!/usr/bin/perluse Tk;my $window = MainWindow->new;$window->title("Welcome to mH-diary.ir");$window->Label(-text => "welcome to my website")->pack;$window->Label(-text => "lets see how can we write something like that")->pack;#$window->Button(-text => "Print a value", -command => \&but )->pack;#$window->Button(-text => "Double the value", -command => sub {$val*=2} )->pack;#$window->Button(-text => "This is a quit button ", -command => \&finito )->pack; $window->Label(-text => "")->pack;$window->Button(-text => "::Exit::",-command => \&m2h )->pack;$window->Button(-text => "About me ",-command => \&m2h )->pack; $window->Label(-text => "")->pack; MainLoop; ############################################### sub but {print "value is $val\n"; } sub m2h { print "test button clicked\n"; } sub finito{ print "going away ...\n"; print "final value is $val\n"; exit; } sub BEGIN { $val = 2;}
there is a screenshot which i found it on my previous website ( in 2008 ).
No comments:
Post a Comment