Project 19A: Programming for Linux? (Absolutely Optional, but Kind of Fun)

 < Day Day Up > 



Project 19A: Programming for Linux? (Absolutely Optional, but Kind of Fun)

I should first say that I am not very keen on programming. I guess my mind is just too jumbled and illogical to deal with it, and I probably had my fill of code after keying in all those Atari games in my early computing days. Nevertheless, many people want to know what it takes to program and what they need to do it.

Most of what you need is included in your Fedora Core system, and there are, in fact, many options. The language that seems to be most popular and that is supposedly easiest for newbies to deal with is called Python. As you may recall, the pyWings oracle, which you installed in Chapter 9, was written in Python.

In order to get a little taste of this programming thing, I’ll give you a couple of sample programs you can play with. The first program in almost any programming book is a silly little thing called “Hello, World.” All the program does is print out the words “Hello, World” to your screen. This always seemed weird to me, as the only person looking at the output of the program is the person who keyed in the code, not “the world.” So let’s change that standard intro program just a bit, and make it more valuable to your self-esteem. Let’s do a “Hey, good looking” program instead. It really is the same thing anyway.

To write the program, open Gedit by going to your Main menu and selecting Accessories > Text Editor. In the Gedit window, type the following two lines — quotation marks too:

print "Hey, good looking!" print "You are looking fine today."

Once you’ve done that, click the Save button, and save the program to your Home folder as goodlooking.py.

You’ve now written a program, be it ever so humble. Now, open up a Terminal window, and run the program by typing python goodlooking.py and pressing ENTER. The output will appear in the Terminal window (see Figure 19-10).

click to expand
Figure 19-10: Running the “Hey, good looking” Python program

Makes you feel kind of good, doesn’t it? Now, if you want to get a tad more carried away, you can create a simple window for this ego-affirming program of yours by using tkinter. As you may recall, you installed tkinter in Chapter 9 for use by the pyWings oracle, which used tkinter for the same purpose.

To write the program so that it will open in a window of its own, open Gedit again, and this time type the following:

from Tkinter import Label, mainloop Label (text='Hey, good looking!\n' 'You are looking fine today.').pack() mainloop()

Note that there are no spaces anywhere after the word today in the third line and no spaces between mainloop and () in the last line.

Save this new program to your Home folder as goodlooking2.py. Once you’ve done that, go back to your Terminal window and type python goodlooking2.py and press ENTER. Your new program will then appear in its own window (see Figure 19-11).


Figure 19-11: The “Hey, good looking” program in its own window

Well, that was all pretty harmless. If you find this programming thing interesting and want to find out more about Python, go to the Python site at www.python.org, which has links to several online tutorials.



 < Day Day Up > 



Linux for Non-Geeks. A Hands-On, Project-Based, Take-It-Slow Guidebook
Linux for Non-Geeks: A Hands-On, Project-Based, Take-It-Slow Guidebook
ISBN: 1593270348
EAN: 2147483647
Year: 2003
Pages: 188

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net