|We don't bite newbies here... much|
RFC: Learning Perl/Tkby perl.j (Pilgrim)
|on Aug 08, 2011 at 15:33 UTC||Need Help??|
Note: I wrote this for a friend and thought you guys should take a look at it. I tried making it as general and easy as possible. Also, please if I have any HTML problems please tell me. Thank You.
UPDATE: Thanks to jdporter, ww, and zentara's great advise I have made changes regarding wording, thoroughness, and choice of HTML tags. I would also like to thank everyone else who gave feedback.
Perl/Tk is probably the most widely known GUI for Perl. It is a great interface used by thousands of people. I am writing this to teach those with no previous knowledge of any programming language or GUI interface. You should know some Perl (you don't need to be an expert, but you need to know some basic/intermediate Perl) before you read this also. Perl/Tk is a module, so it should be fairly easy to get. If you don't know how to install modules, see A Guide to Installing Modules.
Ok. Let's run a simple Perl/Tk program.
Let's take this line by line:
The button widget creates, you guessed it, a button! Some people learn by example. So if you need one, look at my first example. Lines 4-6 create a button that says "Quit". If you press the button, it ends the program. Of course, you could always change the subroutine in the code to make the button do whatever you want (you can also change the text on the button, but you knew knew that :).
The entry widget makes a one-line text box the user can type into. Here's an example:
Frames are very simple container widgets;they act as a container for widgets. Here is a great example of using frames that I found on Bin-Co:
I haven't got to all of the widgets and features in this program yet but we'll get there : ).
Scales are, well, scales. There is not an easy way to explain them so I'll let you see one. Look at the frame on the left in the example for frames. That is a scale where you could pick your age.
Radiobutton & Checkbutton
A radiobutton is used to pick certain choice (usually). Once again I am going to refer you to the example for the frames widget. Do you see where you are supposed to pick a gender. That is a radiobutton.
A checkbutton is just like a radiobutton, it just looks different. If you look at the example for frames, you'll see both. The area where you pick your occupation is the Checkbox.
A listbox displays a list of strings, one per string. To see an example of a listbox, see the frames example. The adding jobs section is a listbox.
There are more widgets (even some that were in the example that I didn't show you) but this should give you a big kick start.
Things You May Not Have Understood
Let's go over some things you may not have understood. First off, pack(). The basic idea of pack()is that any window or widget should be subject to a Tk geometry manager; the packer is one of the placement managers. For more on Geometry Managers,see this webpage.
MainLoop. It is not an easy thing to explain in a small area so I wil refer you to this webpage.
What is a GUI? GUI stands for Graphical User Interface. That is, a user interface based on graphics (icons and pictures and menus) instead of text.
Other Things You Should (and Should Not) Do
1) Never kill the MainLoop! Kill the loop, kill the window.
2) Never use sleep in a GUI program because it will put the loop to sleep and become unresponsive.
Perl/Tk is a powerful, useful, easy-to-use tool. I hope this gave you a good start on how it is used.