faqts : Computers : Programming : Languages : Python : Tkinter

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

19 of 20 people (95%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

I'm trying to capture an Entry box's value and then print it out. How can I do this?

Jun 8th, 2000 09:21
unknown unknown, richard_chamberlain


from Tkinter import *
root=Tk()
entry=Entry(root)
var=StringVar()
entry2=Entry(root,textvariable=var)
entry.pack(side=TOP)
entry2.pack(side=TOP)

def calculate():
    var.set(entry.get())

button=Button(root,text="Calculate",command=calculate)
button.pack(side=RIGHT)
root.mainloop()

Getting and setting an Entry widget is straightforward. In the above 
example I've used entry.get() to get the contents of a widget and I've 
used the textvariable option to set the value.

var=StringVar()

This creates an instance of StringVar that we can assign to our entry.

entry2=Entry(root,textvariable=var)

So every change to var is now reflected in the widget.

var.set(entry.get())

I'm simply changing var to the contents of entry. Obviously here you'll 
need to do some calculations and pass that as a string.

In the Grayson book look at the appendices at the back to check out the
methods and options of Entry.