hi all , I like to write a program which I input the dates and amounts of say 10 checks , and print out the total amount of checks and number of days thet are apart from each other on another entry widget . the code I have done sofar is below , I just don't know how to associate variables to the (month/day) and the amount . is this where I use the IntVar() and StringVar()? please explain these to me too . thanks

from Tkinter import *   # it is easier for me to do it this way
root =Tk()
lable1 =Label(root,text = " please enter the date : mo/day ")
lable1.grid(row=0,sticky=W)
e=Entry(root,takefocus=1).grid(row=0,column=1)

label2 = Label(root,text=" enter the amount " )
label2.grid(row=1,sticky=W)
e1=Entry(root).grid(row=1,column=1)
root.mainloop()

hi all , I like to write a program which I input the dates and amounts of say 10 checks , and print out the total amount of checks and number of days thet are apart from each other on another entry widget . the code I have done sofar is below , I just don't know how to associate variables to the (month/day) and the amount . is this where I use the IntVar() and StringVar()? please explain these to me too . thanks

from Tkinter import *   # it is easier for me to do it this way
root =Tk()
lable1 =Label(root,text = " please enter the date : mo/day ")
lable1.grid(row=0,sticky=W)
e=Entry(root,takefocus=1).grid(row=0,column=1)

label2 = Label(root,text=" enter the amount " )
label2.grid(row=1,sticky=W)
e1=Entry(root).grid(row=1,column=1)
root.mainloop()

The code looks good so far.

There is one feature of your code that you might not intend: e and e1 both have the result of the grid() operation, which is None. I'm guessing that you wanted e and e1 to be your Entry()'s.

Anyways, what needs to happen now is that you need some way to trigger your window to get the items from the Entry()'s. There are two basic ways (AFAIK): either to include an OK button, or else to bind the '<Return>' event to a method of e1. Taking each in turn:

from Tkinter import *   # me too

def update():
    date = e.get()
    amount = e1.get()
    sv.set("%s on %s" % (amount, date))
    
root =Tk()
lable1 =Label(root,text = " please enter the date : mo/day ")
lable1.grid(row=0,sticky=W)
e=Entry(root,takefocus=1)
e.grid(row=0,column=1)

label2 = Label(root,text=" enter the amount " )
label2.grid(row=1,sticky=W)
e1=Entry(root)
e1.grid(row=1,column=1)
b = Button(root,text="OK",command=update)
b.grid(row=2,columnspan=2)
sv = StringVar()
lab = Label(root, textvariable=sv)
lab.grid(row=3,columnspan=2)
root.mainloop()

I'll leave it to you to parse the date and update the total amount. What this does is as follows: the Button b gets associated with the update() command, so that when the user presses OK, update gets called. In turn, update sets the StringVar() sv, which is assigned to the Label() lab.

I gridded both of them with columnspan=2 so that they would be centered.

Here is the second approach, which allows the user to hit Enter after entering his info:

from Tkinter import *  

def update(event):  # <---
    date = e.get()
    amount = e1.get()
    sv.set("%s on %s" % (amount, date))
    
root =Tk()
lable1 =Label(root,text = " please enter the date : mo/day ")
lable1.grid(row=0,sticky=W)
e=Entry(root,takefocus=1)
e.grid(row=0,column=1)

label2 = Label(root,text=" enter the amount " )
label2.grid(row=1,sticky=W)
e1=Entry(root)
e1.grid(row=1,column=1)
e1.bind('<Return>',update)  # <---
sv = StringVar()
lab = Label(root, textvariable=sv)
lab.grid(row=3,columnspan=2)
root.mainloop()

Here, the OK button is replaced with an event binding: when Return (Enter) is pressed, update() is called. Notice the difference in update, however: when a binding is called, it is passed the full event information. Therefore, the handler (update()) has to receive that event. If you want the user to be able to do either one, you can define

def update(event=None):
   ...

and both bind '<Return>' on e1 to update AND set the command for the OK button to update.

Hope it helps,
Jeff

Comments
very good

hi jeff , thanks , your explanation was easy to undrestand , I can do the rest , thanks again
ali

This article has been dead for over six months. Start a new discussion instead.