没有合适的资源?快使用搜索试试~ 我知道了~
首页perl TK 编程指南 (perl_tk_tutorial)
资源详情
资源评论
资源推荐
Perl/Tk Tutorial - Create GUI with Perl's Tk Module
Introduction ......................................................................................................................................................................... 2
Applications ................................................................................................................................................................... 2
Philosophy ...................................................................................................................................................................... 2
Perl/Tk Requirements ..................................................................................................................................................... 3
Installing/Using Perl ....................................................................................................................................................... 3
Hello World ......................................................................................................................................................................... 3
Widgets 1 : Button, Entry, Label ........................................................................................................................................ 5
Button ............................................................................................................................................................................. 6
Entry ............................................................................................................................................................................... 6
Label ............................................................................................................................................................................... 7
Widgets 2 : Frame, Text, Scrollbar, Scale ........................................................................................................................... 8
Frame ............................................................................................................................................................................. 8
Text ................................................................................................................................................................................. 8
Scrollbar ......................................................................................................................................................................... 9
Scale ............................................................................................................................................................................. 11
Dialogs .............................................................................................................................................................................. 12
messageBox .................................................................................................................................................................. 12
chooseColor .................................................................................................................................................................. 13
getOpenFile .................................................................................................................................................................. 13
Toplevel ........................................................................................................................................................................ 14
Widgets 3 : Radiobutton, Checkbutton ............................................................................................................................. 15
Radiobutton .................................................................................................................................................................. 15
Checkbutton .................................................................................................................................................................. 16
Widgets 4 : Listbox ........................................................................................................................................................... 18
Listbox .......................................................................................................................................................................... 18
Widgets 5 : Menubutton, Menu, Optionmenu .................................................................................................................. 20
Menubutton .................................................................................................................................................................. 20
Menu ............................................................................................................................................................................. 21
Optionmenu ................................................................................................................................................................. 23
Some more Widgets - Canvas, Message, Adjuster, Scrolled ............................................................................................ 24
Canvas .......................................................................................................................................................................... 24
Message ........................................................................................................................................................................ 24
Adjuster ........................................................................................................................................................................ 24
Scrolled ........................................................................................................................................................................ 25
Geometry Management : Grid, Pack ................................................................................................................................ 26
grid ............................................................................................................................................................................... 26
pack ............................................................................................................................................................................. 27
Some Common Widget Options ....................................................................................................................................... 28
Some Tk Commands ......................................................................................................................................................... 29
Bind .............................................................................................................................................................................. 29
Now What? ........................................................................................................................................................................ 31
Reference ........................................................................................................................................................................... 31
Books ........................................................................................................................................................................... 31
Manual .......................................................................................................................................................................... 31
External Sites ................................................................................................................................................................ 31
Appendix ........................................................................................................................................................................... 31
Appendix A : About the Author ................................................................................................................................... 31
Appendix B : Commonly Made mistakes in Perl/Tk ................................................................................................... 32
Appendix C : Tcl/Tk And Perl/Tk ................................................................................................................................ 32
Appendix D : Codes ..................................................................................................................................................... 32
Appendix E : FeedBacks .............................................................................................................................................. 33
Appendix F : Comments .............................................................................................................................................. 33
Index ........................................................................................................................................................................ 33
Introduction ............................................................................................................................................................. 33
Hello World ............................................................................................................................................................. 36
Widget 1 .................................................................................................................................................................. 38
Widget 2 .................................................................................................................................................................. 40
Widget 5 .................................................................................................................................................................. 43
Widget 6 .................................................................................................................................................................. 44
Geometry Management ........................................................................................................................................... 44
Now What? .............................................................................................................................................................. 46
Appendix ................................................................................................................................................................. 48
Introduction
Perl/Tk (also known as pTk) is a collection of modules and code that attempts to wed the easily
configured Tk 8 widget toolkit to the powerful lexigraphic, dynamic memory, I/O, and object-oriented
capabilities of Perl 5 In other words, it is an interpreted scripting language for making widgets and
programs with Graphical User Interfaces (GUI)
Perl or Practical Extraction and Report Language is described by Larry Wall, Perl's author, as follows:
"Perl is an interpreted language optimized for scanning arbitrary text files, extracting information from those text files,
and printing reports based on that information It's also a good language for any system management tasks The
language is intended to be practical (easy to use, efficient, complete) rather than beautiful (tiny, elegant, minimal)"
The perlintro man page has this to say
Perl is a general-purpose programming language originally developed for text manipulation and now used
for a wide range of tasks including system administration, web development, network programming, GUI
development, and more
Tk, the extension(or module) that makes GUI programming in perl possible, is taken from Tcl/Tk Tcl(Tool Command
Language) and Tk(ToolKit) was created by Professor John Ousterhout of the University of California, Berkeley Tcl is a
scripting language that runs on Windows, UNIX and Macintosh platforms Tk is a standard add-on to Tcl that provides
commands to quickly and easily create user interfaces Later on Tk was used by a lot of other scripting languages like
Perl, Python, Ruby etc
Applications
Perl has been used since the early days of the web to write CGI scripts, and is now a component of the popular LAMP
(Linux/Apache/MySQL/Perl) platform for web development Perl has been called "the glue that holds the web together"
Large systems written in Perl include Slashdot, and early implementations of Wikipedia and PHP
Perl finds many applications as a glue language, tying together systems and interfaces that were not specifically
designed to interoperate Systems administrators use Perl as an all-purpose tool; short Perl programs can be entered and
run on a single command line
Philosophy
Perl has several mottos that convey aspects of its design and use One is There's more than one way to do it
(TMTOWTDI - usually pronounced 'Tim Toady') Another is Perl: the Swiss Army Chainsaw of Programming
Languages A stated design goal of Perl is to "make easy tasks easy and difficult tasks possible"
Perl is free software, and may be distributed under either the Artistic or the GPL License It is available for most
operating systems but is particularly prevalent on Unix and Unix-like systems (such as Linux, FreeBSD, and Mac OS
X), and is growing in popularity on Microsoft Windows systems
Perl/Tk Requirements
Before starting with the tutorial, make sure you have the following things If some are missing you still can learn perl -
but you will not be able to use it to its full power
1. ActivePerl from http://wwwactivestatecom/ActivePerl/ for windows - for programming in Windows Linux don't
need any special outside interpreter because it already has it in most of the distributions
2. A good text editor I would recommend Crimson Editor(http://wwwcrimsoneditorcom/) for Windows and
XEmacs for Linux
Installing/Using Perl
In Unix/Linux you can execute your perl scripts by typing "perl <filename>" at command prompt But before you do
that make sure you have both Perl and its Tk module Most linux distributions have perl - but quite a few don't have the
Tk module Make sure that the system you are using have the Tk module If you don't have it, go to http://wwwcpanorg
and download the perl module Or you can use the perl's CPAN module to install the Tk module To do this, open a
terminal and enter the following command
perl -MCPAN -e shell
cpan> install Bundle::CPAN
cpan> reload cpan
cpan> install Tk
Another(and a much easier) way to do this is to get a rpm of Perl/Tk and installing it with the command
rpm -ivh FILENAME
If you are using Ubuntu, a easy way of installing Perl/Tk is using this command
sudo apt-get install perl-tk
If you are using Windows, download ActivePerl and install it Then you can execute any perl file by double clicking it
Two more things before we begin the tutorial I will be teaching perl/tk and I expect you to know how to program in perl
I may ignore some of the perl coding conventions like including use strict;, -w or use warnings; in my
examples The examples have only one purpose - to demonstrate the feature that will be taught in that part of the
tutorial Sorry about that - but I have to keep my tutorial's example scripts short and to the point
Finally, this is a tutorial for Perl/Tk only - I will not be teaching perl here So if you know perl, continue But if you are a
beginner to perl, I would recommend that you read my perl tutorial
Hello World
Let us begin, as all other tutorials begin, with the "Hello World" program Create a file called "Hellopl" and enter the
following into it
#!/usr/local/bin/perl
use Tk;
# Main Window
my $mw = new MainWindow;
my $label = $mw -> Label(-text=>"Hello World") -> pack();
my $button = $mw -> Button(-text => "Quit",
-command => sub { exit })
-> pack();
MainLoop;
The first line - #!/usr/local/bin/perl is not needed in windows In Linux, it tells the name of the script
language processor In our case it is perl Don't understand what that means? Don't worry your gray cells over it Just put
it at the top of the file
The second line - use Tk; tells the interpreter that our program will use the Tk module This line is an absolute must
in all GUI programs you make using perl When the interpreter encounters this line, it will load the Tk components that
we will be using to create our program
The third line - This is a comment Any line that starts with a '#' char is a comment Comments are not of any use in the
program It is used by programmer to talk to themselves A programmer cannot be expected to remember every thing a
script does So he uses a comment to write it down Next time he edits the script, he can read the comment and
understand what the program is for It is good practice to make as much comments as possible
The fourth line, my $mw = new MainWindow;, will create a window into which the GUI elements will be placed
The variable $mw is a object of type 'MainWindow' We will have to use this element when we want to place any
widget inside it
The fifth line - $mw -> Label(-text=>"Hello World") -> pack(); makes a label and writes "Hello
world" in it You can change the text to any thing you like Note the structure of the command -
$label - This variable assigned to that particular widget Ever widget must have a UNIQUE variable This name will
be used when ever that widget must be accessed
$mw -> - $mw is the MainWindow's object We will be placing our label widget inside this window
Label(-text=>"Hello World") - 'Label' is the name of the widget A widget is a user interface object in X
graphical user interfaces Confused? Lets just say that it is the name of the object that appears on screen There are
many other widgets too If you want to display a button, you use the button widget For text, you use the text widget For
entry, you guessed it, the entry widget If you want, you can see more about widgets
-text=>"Hello World" - The option for this widget This option says that this widget must be given the text "Hello
World" Options change according to the widgets - a button widget will not have all the options of the label widget and
vise versa But there will be many common ones
Please note that operator used here is '=>' as opposed to the one used earlier '->' in $mw -> One uses the minus(-) sign
while the other uses the equals(=) sign Do not confuse between these two
You can keep writing other options can also be written here For example, let us make a label for showing the text
"Hello World" The other lines are same as the Hello World program
$mw -> Label(-text=>"Hello World",-font=>"courierfont",-relief=>"raised") ->
pack();
In this example, a lot more options are used The font option is used to tell which font must be used to make the text and
the relief option tells whether the text should appear raised, sunken, flat etc To know all the options for a particular
widget, read the manual that comes with Perl It lists every widget and every option they have If you are going to
program in Perl, you will find your self peeking into the manual every few minutes The most important and most
commonly used options are listed here
All options must separated by a comma But as you have noted, this line is a little difficult to read As the number of
options increase, the more difficult to read it So a more readable version is
$mw -> Label(-text=>"Hello World",
-font=>"courierfont",
-relief=>"raised")
-> pack();
Next comes the -> pack(); This will pack the widget '$label' into the window '$mw' 'pack' is a geometry manager
Another geometry manager is 'grid' Personally, I like grid better Once again, putting all this in one line is an eye sore -
so you can put this part in the next line
my $label = $mw -> Label(-text=>"Hello World")
-> pack();
In this case, pack has no options within it But that is not always the case
my $label = $mw -> Label(-text=>"Hello World")
-> pack(-side=>"left",
-anchor=>'w');
You don't have to pack the widget in the same line of creating it - but it is convenient in small programs You can pack
the widget later using the widget's variable For example
my $label = $mw -> Label(-text=>"Hello World"); #We created the widget
$label -> pack(-side=>"left", -anchor=>'w'); #We pack it in another line
So we have the final syntax of how to create and display a widget
my $WidgetVariable = $Window -> WidgetType(?Option 1=>Value 1, ?Option 2=>Value
2 ??) -> pack();
The next three lines
my $button = $mw -> Button(-text => "Quit",
-command => sub { exit })
-> pack();
will create and display a button Here the widget variable is '$button' When we look at the options, we will find two
options - 'text' and 'command' The given text is Quit - so the button will have the text "Quit" on it The command option
determines what should happen when the user click on the button You can specify a function to execute when the user
clicks on the button In this case the program will exit when this button is pressed One can also call functions that you
have created from here
#!/usr/local/bin/perl
use Tk;
# Main Window
my $mw = new MainWindow;
my $label = $mw -> Label(-text=>"Hello World") -> pack();
my $button = $mw -> Button(-text => "Quit",
-command =>\&exitProgam)
-> pack();
MainLoop;
sub exitProgam {
$mw->messageBox(-message=>"Goodbye");
exit;
}
The next line - MainLoop; is the Main Loop or the Event Loop Its job is to invoke callbacks in response to events
such as button presses or timer expirations If this line is missing, the program will run and exit with out waiting for the
user to do any thing This is another one of those 'absolute musts' of Perl/Tk programming
Now Perl puritans will raise a great hue and cry and say that this is not the way to print "Hello World" The "pure"
method is the following
#!/usr/local/bin/perl
print "Hello World"
Putting things in perspective, I am teaching Perl/Tk - not Perl The above is the Perl method of doing it My method is
the pTk method of doing it
Widgets 1 : Button, Entry, Label
A widget is a user interface object in X graphical user interfaces Confused? Lets just say that it is the name of the
object that appears on screen There are many types widgets If you want to display a button, you use the button widget
For text, you use the text widget For entry, you guessed it, the entry widget
Syntax:
my $WidgetVariable = $Window -> WidgetType(?Option 1=>Value 1, ?Option 2=>Value 2 ??) -> pack();
Three things need to be said about widgets First is the widget variable This I have explained earlier The widget
variable of all widgets must be unique and will be used whenever that widget needs to be accessed Second is the
options Each widget has some options which can be used to configure it This is usually done when the widget is
declared, but it can be done afterward also The final thing is commands Each widget has some commands which also
剩余47页未读,继续阅读
xti9er
- 粉丝: 0
- 资源: 18
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- RTL8188FU-Linux-v5.7.4.2-36687.20200602.tar(20765).gz
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
- SPC统计方法基础知识.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论10