Hi,  I'm trying to teach myself Tkinter by making a desktop calculator.

I've got the Tkinter bit working, but getting the output to work how I
want it to is causing serious brainfreeze...

The buttons 0 through 9 and '.' all append to a queue, which
makeNumber(queue) returns as an actual python number.  That all works.

Pressing one of the operators (+, -, /, *, or =) calls the following function:

def evaluate(op):
    """Evaluate a queue of button presses:
    eg: [1, 2, 3, '+', 3, 2, 1] => 123 + 321 = 444"""

    global last_op, total

    initial_total = total
    try:
        operand = makeNumber(queue)
    except ValueError:
        operand = ''

    if last_op == 'add':
        disp_op = '+'
        total += operand if operand != '' else 0

    elif last_op == 'sub':
        disp_op = '-'
        total -= operand if operand != '' else 0

    elif last_op == 'div':
        disp_op = '/'
        total /= float(operand) if operand != '' else 1

    elif last_op == 'mul':
        disp_op = '*'
        total *= operand if operand != '' else 1

    else:
        disp_op = ''
    if int(total) == total:
        total = int(total)

    if last_op == '':
        print operand,
    else:
        print initial_total, disp_op, operand, '=', total
    last_op = op
    clearqueue()

What I want it to do is something like:
(the first list indicates button presses NOT the contents of the queue
[1, 2, 3, '+', 3, 2, 1, '*' 2] =>
queue = [1,2,3]; evaluate('add') gives output:
123 + 321 = 444
queue = [3,2,1]; evaluate('mul') gives output:
444 * 2 = 888

Like I said, the makeNumber function works, and the numbers are
definatly getting appended to the queue.  The logic within the above
function is the problem.  The main problem is allowing button
sequences like:
number op number equals, number op number equals


Did that make any sense at all?
-- 
Richard "Roadie Rich" Lovely, part of the JNP|UK Famile
www.theJNP.com
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to