entry widget

Reply

Join Date: Jan 2007
Posts: 23
Reputation: fonzali is an unknown quantity at this point 
Solved Threads: 0
fonzali fonzali is offline Offline
Newbie Poster

entry widget

 
0
  #1
Mar 14th, 2007
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
  1. from Tkinter import * # it is easier for me to do it this way
  2. root =Tk()
  3. lable1 =Label(root,text = " please enter the date : mo/day ")
  4. lable1.grid(row=0,sticky=W)
  5. e=Entry(root,takefocus=1).grid(row=0,column=1)
  6.  
  7. label2 = Label(root,text=" enter the amount " )
  8. label2.grid(row=1,sticky=W)
  9. e1=Entry(root).grid(row=1,column=1)
  10. root.mainloop()
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 152
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: entry widget

 
1
  #2
Mar 14th, 2007
Originally Posted by fonzali View Post
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
  1. from Tkinter import * # it is easier for me to do it this way
  2. root =Tk()
  3. lable1 =Label(root,text = " please enter the date : mo/day ")
  4. lable1.grid(row=0,sticky=W)
  5. e=Entry(root,takefocus=1).grid(row=0,column=1)
  6.  
  7. label2 = Label(root,text=" enter the amount " )
  8. label2.grid(row=1,sticky=W)
  9. e1=Entry(root).grid(row=1,column=1)
  10. 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:

  1. from Tkinter import * # me too
  2.  
  3. def update():
  4. date = e.get()
  5. amount = e1.get()
  6. sv.set("%s on %s" % (amount, date))
  7.  
  8. root =Tk()
  9. lable1 =Label(root,text = " please enter the date : mo/day ")
  10. lable1.grid(row=0,sticky=W)
  11. e=Entry(root,takefocus=1)
  12. e.grid(row=0,column=1)
  13.  
  14. label2 = Label(root,text=" enter the amount " )
  15. label2.grid(row=1,sticky=W)
  16. e1=Entry(root)
  17. e1.grid(row=1,column=1)
  18. b = Button(root,text="OK",command=update)
  19. b.grid(row=2,columnspan=2)
  20. sv = StringVar()
  21. lab = Label(root, textvariable=sv)
  22. lab.grid(row=3,columnspan=2)
  23. 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:

  1. from Tkinter import *
  2.  
  3. def update(event): # <---
  4. date = e.get()
  5. amount = e1.get()
  6. sv.set("%s on %s" % (amount, date))
  7.  
  8. root =Tk()
  9. lable1 =Label(root,text = " please enter the date : mo/day ")
  10. lable1.grid(row=0,sticky=W)
  11. e=Entry(root,takefocus=1)
  12. e.grid(row=0,column=1)
  13.  
  14. label2 = Label(root,text=" enter the amount " )
  15. label2.grid(row=1,sticky=W)
  16. e1=Entry(root)
  17. e1.grid(row=1,column=1)
  18. e1.bind('<Return>',update) # <---
  19. sv = StringVar()
  20. lab = Label(root, textvariable=sv)
  21. lab.grid(row=3,columnspan=2)
  22. 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

  1. def update(event=None):
  2. ...

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

Hope it helps,
Jeff
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 23
Reputation: fonzali is an unknown quantity at this point 
Solved Threads: 0
fonzali fonzali is offline Offline
Newbie Poster

Re: entry widget

 
0
  #3
Mar 15th, 2007
hi jeff , thanks , your explanation was easy to undrestand , I can do the rest , thanks again
ali
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2630 | Replies: 2
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC