Danh mục

Web Client Programming with Perl-Chapter 7: Graphical Examples with Perl/Tk- P1

Số trang: 14      Loại file: pdf      Dung lượng: 44.62 KB      Lượt xem: 10      Lượt tải: 0    
10.10.2023

Hỗ trợ phí lưu trữ khi tải xuống: 3,000 VND Tải xuống file đầy đủ (14 trang) 0
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Tham khảo tài liệu web client programming with perl-chapter 7: graphical examples with perl/tk- p1, công nghệ thông tin, quản trị web phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Web Client Programming with Perl-Chapter 7: Graphical Examples with Perl/Tk- P1 Chapter 7: Graphical Examples with Perl/Tk- P1The Tk extension to Perl can be used to create a Graphical User Interface(GUI) to your Perl programs on UNIX. Why would you want to do this?Several reasons, such as ease of use, or to be able to display HTML nicely.Instead of just writing a cool script, you could go as far as writing yourown custom browser.In this chapter, we show a few examples of Tk-based web clients, which gobeyond the command-line interface that weve been using so far in thisbook:[1]  xword, a dictionary client  track, a graphical version of the FedEx example shown in Chapter 6.  webping, an at-a-glance display of the status of multiple web serversOne caveat about Tk, and its a serious one. At this writing, the Tk module toPerl (also known as pTk) only runs on UNIX machines with the X WindowSystem. While the Tk extension to the Tcl language has been successfullyported to Microsoft Windows, the Perl port is still pending, although it isrumored to be in the works.Still, even with its limited availability, we think the ability to give yourprograms an easy-to-use graphical interface is important enough to devote achapter to it. And who knows--by the time youre reading this, the pTk portto Windows might already be completed, and this whole paragraph may bemoot.A Brief Introduction to TkTk was originally developed by John Ousterhout as an extension to his Tcllanguage, for providing a graphical user interface for the X Window System.It was ported to Perl soon afterwards; Nick Ing-Simmons did most of thework to make it functional as a module with Perl. You can get Tk from anyCPAN archive (http://www.perl.com/CPAN/).The Tk extension provides an easy way to draw a window, put widgets intoit (such as buttons, check boxes, entry fields, menus, etc.), and have themperform certain actions based on user input. A simple Hello Worldprogram would look like this:1 #!/usr/bin/perl -w2 use Tk;3 my $mw = MainWindow->new;4 $mw->Button(-text => Hello World!, -command=>sub{exit})->pack;5 MainLoop;(The line numbers are not part of the actual code; they are just included forease in reference.)When you run it, it would look like Figure 7-1.Figure 7-1. A simple Tk widgetPushing the Hello World button will exit the program, and your windowwill then go away. Line 1 tells the shell to invoke Perl to interpret the rest ofthe file, and Line 2 then tells Perl that we need to use the Tk module. Line 3tells the system that you want it to build you a generic, standard window.Line 4 creates a button, displays it (using the pack method), and gives thebutton something to do when pushed.Line 5 tells the program to go do it. MainLoop kicks off the event handlerfor the graphical interface. The most important concept to understand withPerl/Tk is that the program wont do a single thing until it hits the MainLoopstatement. You wont see any graphical output at all until then. We prepare itby telling it what we want to draw, and what should happen when certainevents happen, such as a mouse click on our button in our Hello Worldprogram. The more complex the things you want the GUI to do, the morecomplex the code looks for setting it up.Since the purpose of this chapter is to show some examples using Tk and tointeract with the WWW, we wont be going into much more detail aboutwhat Tk does and why. Some places you might look for help are thenewsgroup comp.lang.perl.tk for Perk/Tk-specific questions, or the Perl/TkFAQ at http://w4.lns.cornell.edu/~pvhp/ptk/ptkFAQ.html. Any search sitewill point you to at least 30 web sites as well. And of course the Tk sourceincludes pod documentation: run pod2text on Tk.pm to get started.Before we continue, there a few odd things you need to know about Perl/Tk:  => is functionally the same as a comma (,). Using => makes it easier to detect pairs of items in a list.  Widgets are always built referencing another part of the GUI, if not the main window (in our examples, $mw), then another widget or frame. This builds the parent/child hierarchy and allows the packer to know what to pack where.  The pack( ) method essentially displays the widget on the screen, according to any parameters sent to it. Alternately, it could un-display it as well. If you dont pack( ) a widget, it wont show up.Now on to some examples.A Dictionary Client: xwordFor our first example, we want to build a simple application that has only afew types of widgets in it. The xword program will prompt the user for aword, then use an online dictionary to define it, and return the formattedresults.When you need a quick word definition, instead of running a web browser(which can often have a lengthy startup time with all those fancy plug-ins),surfing to the site via a bookmark, and then entering the word to get youranswer, you can use this simple program that will just prompt for the wordand go look it up without all that extra hassle. Anyone familiar with thexwebster client for the X Window System will find xword to be vaguelyfamiliar, but our version doesnt require a local licensed dictionary server;we use one already existing on the Web. Since the program is so simple, youcan probably just iconify it, and then bring it back up whenever yourestumped for the spelling or meaning of another word.So in designing our window, we want a place to enter the word, and a placeto display the results. We also need to be able to exit the program (always amust). It seems pretty simple, until we remember that the definitioninformation sent back to us is going to come back in HTML. I really dontwant to have to visually dig through a bunch of HTML codes to find out theans ...

Tài liệu được xem nhiều: