[Tutor] Buttons
Alan Gauld
alan.gauld at blueyonder.co.uk
Sun Feb 1 18:05:37 EST 2004
> I have a cash program that I am trying to create
> buttons for instead of a simple number menu. I
> understand that I need to import the Tkinter, create
> and position the widgets, and enter the main loop
Yes, but of course you aren't doing any of that here...
> (Python in a nut shell). But I am still having
> problems. I have attached the code
It would help if you gave us a clue as to what the
problems were? They obviously are not related to the
Buttons since there is no Tkinter code here.
I'll try to add some comments....
> class AccountTransaction(object):
> def __init__(self, amount, time):
> self.amount = amount
> self.time = time
Not sure why you are using a new style class here,
especially given that you use old stuyle classes later on.
But it shouldn't cause any real problems.
> def __repr__(self):
> description = time.ctime(self.time) + ' '
> if self.amount < 0:
> description += '-'
> description += '$' + str(abs(self.amount))
> return description
This would be much easier with a format string, something like:
description += "%s$%05.2s" % (sign, self.amount)
Set the sign using the code above or the simpler(IMHO!):
sign = (sign < 0 and '-') or ''
> def __init__(self, initial):
> self.balance = amount
Not sure why we have two init methods? This will replace the
first one. If you come from Java or C++ maybe you are used
to multiple constructors but you can't do that in Python, sorry.
> while 1:
> print
> print
> print 'Show Me The Money'
> print '1. Deposite'
> print '2. Withdraw'
> print '3. Getbalance'
> print '9. Quit'
> print
> choice = int(raw_input('Enter your choice: '))
Lots of print statements are usually better done with a triple
quoted string and/or a format string. In this case:
print '''
Show Me The Money
1. Deposit
2. Withdraw
3. Getbalance
9. Quit
'''
> # I added the exception line so that negartive
> numbers woudl not be added.
>
> if choice == 1:
> amount = raw_input ('Amount: ')
> elif amount < 0:
> raise ValueError, 'Deposites must be positive'
This only gets called if choice is not 1. That is it will not
check the value input by the user. Even if it did you would
be comparing a string and a number, you need to convert the
raw_input() value to an int.
> elif self.balance - amount < 0:
Again, this only gets called if choice is not 1 and amount
is positive. I suspect you want these tests all to be plain
if statements nested under the if choice === 1?.
> date = raw_input ('Date: ')
> numbers[amount] = date
If you are storing a date why not convert it to a date
and store that? Its probably easier to manipulate later.
There is a third party date module (mxdate?) that you might
find easier to use that the standard time module.
> elif choice == 2:
> amount = raw_input ('Amount: ')
> if amount < 0:
> raise ValueError, 'Withdrawals must be
This will mostly work as you expect, but mainly by accident.
If none of the tests above are true then you will do the choice
test, then because the elif chain is stopped ypu will do the
'if amount' test
I suspect the structure you really want is:
if choice == 1:
# get the amount
# if amount < 0 : raise error
# elif balance-amount < 0 : raise error
elif choice == 2:
# get amount
# if amount < 0: raise error
# etc
elif choice == 3:
# and so on.
elif choice == 9:
# and here
else:
# finally...
> class Account:
> def Deposit(self, amt):
> self.balance = self.balance + amt
> def Withdraw(self,amt):
> self.balance = self.balance - amt
> def getbalance(self):
> return self.balance
You never set balance(no init method?) so any attempt to call
the methods will fail because self.balance has not been
defined before use.
HTH,
Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld
More information about the Tutor
mailing list