943,872 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 3993
  • Python RSS
Mar 14th, 2007
0

entry widget

Expand 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
python Syntax (Toggle Plain Text)
  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()
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
fonzali is offline Offline
25 posts
since Jan 2007
Mar 14th, 2007
1

Re: entry widget

Click to Expand / Collapse  Quote originally posted by fonzali ...
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
python Syntax (Toggle Plain Text)
  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:

Python Syntax (Toggle Plain Text)
  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:

Python Syntax (Toggle Plain Text)
  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

Python Syntax (Toggle Plain Text)
  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
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Mar 15th, 2007
0

Re: entry widget

hi jeff , thanks , your explanation was easy to undrestand , I can do the rest , thanks again
ali
Reputation Points: 10
Solved Threads: 0
Light Poster
fonzali is offline Offline
25 posts
since Jan 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: HOw to register and unregister variables in python
Next Thread in Python Forum Timeline: Storing a Template String?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC