Python GUI Tk : TextField Entry

 Bind enter key to Entry

 

#!/usr/bin/env python
from Tkinter import *
import math

root = Tk()     
top = Frame(root)
top.pack(side='top')

hwtext = Label(top, text='Hello, World! The sine of')
hwtext.pack(side='left')

r = StringVar() 
r.set('1.2')    
r_entry = Entry(top, width=6, textvariable=r)
r_entry.pack(side='left')

s = StringVar() 
def comp_s(event):
    global s
    s.set('%g' % math.sin(float(r.get()))) # construct string

r_entry.bind('<Return>', comp_s)

compute = Label(top, text=' equals ')
compute.pack(side='left')

s_label = Label(top, textvariable=s, width=18)
s_label.pack(side='left')

root.mainloop()

Entry (Text field) with a label inside a border panel

from Tkinter import *

class AllTkinterWidgets:
    def __init__(self, master):
        frame = Frame(master, width=500, height=400, bd=1)
        frame.pack()

        iframe2 = Frame(frame, bd=2, relief=RIDGE)
        Label(iframe2, text='Label:').pack(side=LEFT, padx=5)
        t = StringVar()
        Entry(iframe2, textvariable=t, bg='white').pack(side=RIGHT, padx=5)
        t.set('Entry widget')
        iframe2.pack(expand=1, fill=X, pady=10, padx=5)


    
root = Tk()
all = AllTkinterWidgets(root)
root.title('Tkinter Widgets')
root.mainloop()

Use Entry

from Tkinter import *

root = Tk()

root.title('Entry')
Label(root, text="Label:").pack(side=LEFT, padx=5, pady=10)
e = StringVar()
Entry(root, width=40, textvariable=e).pack(side=LEFT)
e.set("'Text Text Text Text'")
root.mainloop()
Entry: TextField: get entered value
from Tkinter import *

from tkMessageBox import askokcancel           

class Quitter(Frame):                          
    def __init__(self, parent=None):           
        Frame.__init__(self, parent)
        self.pack()
        widget = Button(self, text='Quit', command=self.quit)
        widget.pack(expand=YES, fill=BOTH, side=LEFT)
    def quit(self):
        ans = askokcancel('Verify exit', "Really quit?")
        if ans: Frame.quit(self)


def fetch():
    print 'Input => "%s"' % ent.get()          

root = Tk()
ent = Entry(root)
ent.insert(0, 'Type words here')               
ent.pack(side=TOP, fill=X)                     

ent.focus()                                    
ent.bind('<Return>', (lambda event: fetch()))  
btn = Button(root, text='Fetch', command=fetch) 
btn.pack(side=LEFT)
Quitter(root).pack(side=RIGHT)
root.mainloop()

Entry: enter event

from Tkinter import *

from tkMessageBox import askokcancel           

class Quitter(Frame):                          
    def __init__(self, parent=None):
        Frame.__init__(self, parent)
        self.pack()
        widget = Button(self, text='Quit', command=self.quit)
        widget.pack(expand=YES, fill=BOTH, side=LEFT)
    def quit(self):
        ans = askokcancel('Verify exit', "Really quit?")
        if ans: Frame.quit(self)


def fetch():
    print 'Input => "%s"' % ent.get()              # get text
    #ent.delete('0', END)
    ent.insert(END, 'x')
    ent.insert(0, 'x')

root = Tk()
ent = Entry(root)
ent.insert(0, 'Type words here')    
ent.pack(side=TOP, fill=X)          

ent.focus()                         
ent.bind('<Return>', (lambda event: fetch()))      
btn = Button(root, text='Fetch', command=fetch)    
btn.pack(side=LEFT)
Quitter(root).pack(side=RIGHT)
root.mainloop()

Use Entry widgets directly and layout by rows

from Tkinter import *

from tkMessageBox import askokcancel          

class Quitter(Frame):                         
    def __init__(self, parent=None):          
        Frame.__init__(self, parent)
        self.pack()
        widget = Button(self, text='Quit', command=self.quit)
        widget.pack(expand=YES, fill=BOTH, side=LEFT)
    def quit(self):
        ans = askokcancel('Verify exit', "Really quit?")
        if ans: Frame.quit(self)


fields = 'First Name', 'Last Name', 'Job'

def fetch(entries):
    for entry in entries:
        print 'Input => "%s"' % entry.get()   

def makeform(root, fields):
    entries = []
    for field in fields:
        row = Frame(root)                     
        lab = Label(row, width=5, text=field) 
        ent = Entry(row)
        row.pack(side=TOP, fill=X)            
        lab.pack(side=LEFT)
        ent.pack(side=RIGHT, expand=YES, fill=X)
        entries.append(ent)
    return entries

if __name__ == '__main__':
    root = Tk()
    ents = makeform(root, fields)
    root.bind('<Return>', (lambda event, e=ents: fetch(e)))   
    Button(root, text='Fetch',
                 command=(lambda e=ents: fetch(e))).pack(side=LEFT)
    Quitter(root).pack(side=RIGHT)
    root.mainloop()
Entry Fields in a row
from Tkinter import *

fields = 'First Name', 'Last Name', 'Job'

def fetch(entries):
    for entry in entries:
        print 'Input => "%s"' % entry.get()

def makeform(root, fields):
    entries = []
    for field in fields:
        row = Frame(root)                  
        lab = Label(row, width=5, text=field)
        ent = Entry(row)
        row.pack(side=TOP, fill=X)           
        lab.pack(side=LEFT)
        ent.pack(side=RIGHT, expand=YES, fill=X)
        entries.append(ent)
    return entries

def show(entries):
    fetch(entries)
    popup.destroy()

def ask():
    global popup
    popup = Toplevel()
    ents = makeform(popup, fields)
    Button(popup, text='OK', command=(lambda e=ents: show(e)) ).pack()
    popup.grab_set()
    popup.focus_set()
    popup.wait_window()

root = Tk()
Button(root, text='Dialog', command=ask).pack()
root.mainloop()


Get value from Entry

from Tkinter import *
from tkMessageBox import askokcancel           

class Quitter(Frame):                          
    def __init__(self, parent=None):           
        Frame.__init__(self, parent)
        self.pack()
        widget = Button(self, text='Quit', command=self.quit)
        widget.pack(expand=YES, fill=BOTH, side=LEFT)
    def quit(self):
        ans = askokcancel('Verify exit', "Really quit?")
        if ans: Frame.quit(self)

fields = 'First Name', 'Last Name', 'Job'

def fetch(variables):
    for variable in variables:
        print 'Input => "%s"' % variable.get()      

def makeform(root, fields):
    form = Frame(root)                              
    left = Frame(form)
    rite = Frame(form)
    form.pack(fill=X) 
    left.pack(side=LEFT)
    rite.pack(side=RIGHT, expand=YES, fill=X)

    variables = []
    for field in fields:
        lab = Label(left, width=5, text=field)
        ent = Entry(rite)
        lab.pack(side=TOP)
        ent.pack(side=TOP, fill=X)
        var = StringVar()
        ent.config(textvariable=var)
        var.set('enter here')
        variables.append(var)
    return variables

if __name__ == '__main__':
    root = Tk()
    vars = makeform(root, fields)
    Button(root, text='Fetch', 
                 command=(lambda v=vars: fetch(v))).pack(side=LEFT)
    Quitter(root).pack(side=RIGHT)
    root.bind('<Return>', (lambda event, v=vars: fetch(v)))   
    root.mainloop()

Set textvariable for Entry

 
#!/usr/bin/env python
from Tkinter import *
import math

root = Tk()     
top = Frame(root)
top.pack(side='top') 

hwframe = Frame(top)
hwframe.pack(side='top')
font = 'times 18 bold'
hwtext = Label(hwframe, text='Hello, World!', font=font)
hwtext.pack(side='top', pady=20)

rframe = Frame(top)
rframe.pack(side='top', padx=10, pady=20)

r_label = Label(rframe, text='The sine of')
r_label.pack(side='left')

r = StringVar() 
r.set('1.2')    
r_entry = Entry(rframe, width=6, textvariable=r)
r_entry.pack(side='left')

s = StringVar() 
def comp_s(event=None):
    global s
    s.set('%g' % math.sin(float(r.get()))) # construct string

r_entry.bind('<Return>', comp_s)

compute = Button(rframe, text=' equals ', command=comp_s,
                 relief='flat')
compute.pack(side='left')

s_label = Label(rframe, textvariable=s, width=12)
s_label.pack(side='left')

def quit(event=None):
    root.destroy()
quit_button = Button(top, text='Goodbye, GUI World!', command=quit,
                     background='yellow', foreground='blue')
quit_button.pack(side='top', pady=5, fill='x')
root.bind('<q>', quit)

root.mainloop()