| | |
entry widget
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2007
Posts: 23
Reputation:
Solved Threads: 0
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)
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()
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
•
•
•
•
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)
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()
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)
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:
Python Syntax (Toggle Plain Text)
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
Python Syntax (Toggle Plain Text)
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
![]() |
Similar Threads
- Entry Widget Probelms (Python)
- Text widget puzzles. (Python)
- Invisible entry widget. (Python)
- Tkinter: protecting text in the Entry widget (Python)
Other Threads in the Python Forum
- Previous Thread: HOw to register and unregister variables in python
- Next Thread: Storing a Template String?
| Thread Tools | Search this Thread |
alarm assignment avogadro beginner bluetooth character cmd code customdialog cx-freeze data decimals dictionary directory dynamic error examples excel exe file float font format function generator gnu graphics gui halp homework http ideas import input itunes java leftmouse line linux list lists logging loop maintain maze module mouse number numbers output parsing path port prime programming projects push py2exe pygame pyglet pyqt python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation sqlite ssh stdout string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode urllib urllib2 variable ventrilo verify vigenere web webservice wikipedia windows wxpython xlib





