Dies ist die Support Website des Buches:

Das Python Praxisbuch
Der große Profi-Leitfaden für Programmierer
Farid Hajji
Addison Wesley / Pearson Education
ISBN 978-3-8273-2543-3 (Sep 2008), 1298 Seiten.

3. Hello, World!

Das Hello, World!-Programm

#!/usr/bin/env python
# hello.py -- the traditional hello world program.

print "Hello, World!"

name = raw_input("What's your name? ")
print "Nice to meet you,", name

hello.py

hello.py verstehen

hello.py unter Unix ausführen

URLs:

#!/usr/bin/env python
# pyversion.py -- print current version of python

import sys

print sys.version, sys.prefix

pyversion.py

hello.py unter Windows ausführen

hello.py in IDLE ausführen

Screenshots:

hello2.py mit sys.argv

#!/usr/bin/env python
# hello2.py -- the traditional hello world program, cli version.

'''
This program greets the user and asks him for a name,
but only if the name has not been specified on the
command line interface as its first argument. Then
it welcomes the user with a nice personalized message.

Call this program either as:
  hello2.py "John Doe"
or as
  hello2.py
'''

import sys

def say_hello():
    "Say hello to the world"
    print "Hello, World!"

def ask_user_from_cli():
    "Fetch user name from the command line interface"
    if len(sys.argv) > 1:
        return sys.argv[1]
    else:
        return None

def ask_user_interactively():
    "Ask user for his name"
    return raw_input("What's your name? ")

def greet_user(name):
    "Send user a personalized greeting"
    print "Nice to meet you,", name

def main():
    "This is the main program"
    say_hello()
    name = ask_user_from_cli()
    if name is None:
        name = ask_user_interactively()
    greet_user(name)

if __name__ == '__main__':
    main()

hello2.py

hello2.py verstehen

hello2.py ausführen

hello2.py unterm Debugger

tkhello.py mit Tkinter

Screenshots:

#!/usr/bin/env python
# tkhello.py -- Hello, World as a Tkinter application

import sys
from Tkinter import *

def build_gui():
    "Build the GUI. Return root, entry, and personalized greeting label"

    rootWindow = Tk()
    rootWindow.wm_geometry("500x200")
    
    label1 = Label(rootWindow)
    label1['text'] = "Hello, Tkinter World!"
    label1.pack()
    
    label2 = Label(rootWindow)
    label2['text'] = "What's your name?"
    label2.pack()

    nameEntry = Entry(rootWindow)
    nameEntry.bind('<Key-Return>', entry_callback)
    nameEntry.pack(fill=X)

    okButton = Button(rootWindow)
    okButton['text'] = 'OK'
    okButton['command'] = entry_callback
    okButton.pack(fill=X)
    
    exitButton = Button(rootWindow)
    exitButton['text'] = 'Exit'
    exitButton['command'] = exit_callback
    exitButton.pack(fill=X)

    outLabel = Label(rootWindow)
    outLabel['text'] = ''
    outLabel.pack()
    
    return rootWindow, nameEntry, outLabel

def entry_callback(event=None):
    "Called when the Return key is hit in the entry field or OK is clicked"
    name = theEntry.get()
    theLabel['text'] = "Nice to meet you, %s" % name

def exit_callback():
    "Called when the Exit button is hit"
    sys.exit(0)

def main():
    global theRoot, theEntry, theLabel
    theRoot, theEntry, theLabel = build_gui()
    theRoot.mainloop()

if __name__ == '__main__':
    main()

tkhello.py

Zusammenfassung