LJ Archive

Listing 1: calc.py

from java.awt import * from pawt.swing import *

labels = ['7', '8', '9', '+',
    '4', '5', '6', '-',
    '1', '2', '3', '*',
    '0', '.', '=', '/']

keys = JPanel(GridLayout(4, 4))
display = JTextField()
def push(event):  # Callback for regular keys
    display.replaceSelection(event.actionCommand)
def enter(event): # Callback for '=' key
    display.text = str(eval(display.text))
    display.selectAll()
for label in labels:
    key = JButton(label)
    if label == '=':
        key.actionPerformed = enter
    else:
        key.actionPerformed = push
    keys.add(key)

panel = JPanel(BorderLayout())
panel.add("North", display)
panel.add("Center", keys)
test(panel)
LJ Archive