[Tutor] My code works but it is a little bit broken.

brandon w bbbgggwww at gmail.com
Mon Sep 26 04:25:06 CEST 2011


This code calculates money. The problem is that in the first field after the
mouse enters a second time updates itself with the value from the last
field. I don't want it to do that.
I think that the problem is in the "callback" method.

#!/usr/bin/python

from Tkinter import *


root = Tk()
root.title("Money Calculator")
root.geometry("400x300")
root.grid()

def callback(event):
    field0 = varText1.get()
    initAmount.delete(0, END)
    initAmount.insert(0, field0)
    return

def calculateAmount():
    field1 = varText.get()
    weekly_sum.delete(0, END)
    weekly_sum.insert(0, int(field1 * 7))
    field2 = varText.get()
    monthly_sum.delete(0, END)
    monthly_sum.insert(0, int(field2 * 31))
    field3 = varText.get()
    yearly_sum.delete(0, END)
    yearly_sum.insert(0, int(field3 * 356))
    field4 = varText.get()
    five_year_sum.delete(0, END)
    five_year_sum.insert(0, int((field4 * 1825) + 1))
    field5 = varText.get()
    ten_year_sum.delete(0, END)
    ten_year_sum.insert(0, int((field5 * 3650) + 1))
    return

Label(root, text="Inital amount: $").grid(sticky=W, row=0)

varText = IntVar()
varText.set(0)
initAmount = Entry(root, textvariable=varText, width=10, bg = "pale green")
initAmount.bind("<Enter>", callback)
initAmount.grid(sticky=W, row=0, column=1)

Label(root, text="dollars").grid(sticky=W, column=2, row=0)

varText1 = StringVar(None)
Entry(root, textvariable=varText1, bg = "pale green").grid(sticky=W, row=1,
columnspan=3)

Label(root, text=".").grid(sticky=W, column=3, row=1)

Label(root, text="Calculated amount: ").grid(sticky=W, row=3, columnspan=3)

Button(root, text="Calculate", width=10,
command=calculateAmount).grid(sticky=W, row=2, columnspan=3)

Label(root, text="a week").grid(sticky=W, row=4, column=0)

varText1 = StringVar(None)
weekly_sum = Entry(root, textvariable=varText1, takefocus=0, width=15, bg =
"pale green")
weekly_sum.grid(sticky=W, row=4, column=2)

Label(root, text="a month").grid(sticky=W, row=5, columnspan=3)

varText1 = StringVar(None)
monthly_sum = Entry(root, textvariable=varText1, takefocus=0, width=15, bg =
"pale green")
monthly_sum.grid(sticky=W, row=5, column=2)

Label(root, text="a year").grid(sticky=W, row=6, columnspan=3)

varText1 = StringVar(None)
yearly_sum = Entry(root, textvariable=varText1, takefocus=0, width=15, bg =
"pale green")
yearly_sum.grid(sticky=W, row=6, column=2)

Label(root, text="five years").grid(sticky=W, row=7, columnspan=3)

varText1 = StringVar(None)
five_year_sum = Entry(root, textvariable=varText1, takefocus=0, width=15, bg
= "pale green")
five_year_sum.grid(sticky=W, row=7, column=2)

Label(root, text="ten years").grid(sticky=W, row=8, columnspan=3)

varText1 = StringVar(None)
ten_year_sum = Entry(root, textvariable=varText1, takefocus=0, width=15, bg
= "pale green")
ten_year_sum.grid(sticky=W, row=8, column=2)


root.mainloop()


Python 2.6.6
Linux
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110925/aa2bd558/attachment-0001.html>


More information about the Tutor mailing list