| | |
entry widget
![]() |
•
•
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: 152
•
•
•
•
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?
Views: 2630 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Python
application array beginner c++ c/c++ change character class client cmd code command convert count create ctypes curved database dictionary django dll error examples excel exe extensions fdlib file float format framework ftp function graphics gui homework image images import input library line linux list lists logging loop loops microcontroller mouse movingimageswithpygame mysql mysqldb number numbers output parse parsing path port prime processing program programming py2exe pygame pygtk pyqt python random raw_input recursion recursive redirect remote scrolledtext server socket ssh stamp stdout string strings table terminal text thread threading tkinter toolbar transparency tuple tutorial ubuntu unicode variable variables web windows wxpython





