Entry widgets are used to get input, i.e. text strings, from the user of an application. This widget only allows the user to enter a single line of text in a single font. Thus the Entry widget is a standard Tkinter widget used to enter or display a single line of text. To enter multiple lines of text, the Text widget is used.
The syntax of an entry widget looks like this:
w = Entry(master, option, ... )
"master" represents the parent window, where the entry widget should be placed. Like other widgets, it's possible to further influence the rendering of the widget by using options. The comma separated list of options can be empty.
The following program creates an application with two entry fields, one for entering a last name and one for the first name:
master = Tk()
Label(master, text="First Name").grid(row=0)
Label(master, text="Last Name").grid(row=1)
e1 = Entry(master)
e2 = Entry(master)
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
mainloop( )
The output of this program is shown below:
The GUI window that Tkinter creates says tk by default. To change it use the following syntax:
The GUI window that Tkinter creates says Tk by default. Here is how to change it:
master.title('Your title'). Thus we'll change the above Entry widget name to 'Name' by adding
master.title('Name'). The output now looks like:
The above program just demonstrates the creation of an entry widget. To add entry text to the widget, we use the insert method. To replace the current text, we call delete before inserting the new text. To fetch the current entry text, we use the get method.
Now let us modify our first program and get entry from user. We will also incorporate two buttons, one to display the entered text and another to quit the application. The program is shown below:
from tkinter import *
def show_entry_fields():
print("First Name: %s\nLast Name: %s" % (e1.get(), e2.get()))
master = Tk()
master.title('Name')
Label(master, text="First Name").grid(row=0)
Label(master, text="Last Name").grid(row=1)
e1 = Entry(master)
e2 = Entry(master)
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
Button(master, text='Exit', command=master.quit).grid(row=3, column=0, sticky=W, pady=4)
Button(master, text='Display', command=show_entry_fields).grid(row=3, column=1, sticky=W, pady=4)
mainloop( )
The output of the program is shown below:
Once we enter text and click display the entered text is shown in the command window:
If the user wants to quit the application he clicks exit button. Should you wish to have a default text in the entry widget use the insert() method and to delete the content of entry object use the delete() method as shown in the example below:
from tkinter import *
def show_entry_fields():
print("First Name: %s\nLast Name: %s" % (e1.get(), e2.get()))
e1.delete(0,END)
e2.delete(0,END)
master = Tk()
master.title('Name')
Label(master, text="First Name").grid(row=0)
Label(master, text="Last Name").grid(row=1)
e1 = Entry(master)
e2 = Entry(master)
e1.insert(10,"Veevaeck")
e2.insert(10,"Swami")
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
Button(master, text='Exit', command=master.quit).grid(row=3, column=0, sticky=W, pady=4)
Button(master, text='Display', command=show_entry_fields).grid(row=3, column=1, sticky=W, pady=4)
mainloop( )
The output of the program is shown below:
The default text was cleared manually and we entered new text which is then displayed in the command window. The program is ready for new entry with empty entry fields. Sometimes we want to disable a button so it can’t be clicked. Buttons have an attribute state that allows you to disable the widget. Use state=DISABLED to disable the button and state=NORMAL to enable it. The following program implements this functionality so that in the beginning the application's Exit button is disabled but once the entry is submitted it get's enabled:
from tkinter import *
def show_entry_fields():
print("First Name: %s\nLast Name: %s\n" % (e1.get(), e2.get()))
e1.delete(0,END)
e2.delete(0,END)
b1.configure(state=NORMAL)
master = Tk()
master.title('Name')
Label(master, text="First Name").grid(row=0)
Label(master, text="Last Name").grid(row=1)
e1 = Entry(master)
e2 = Entry(master)
e1.insert(10,"Veevaeck")
e2.insert(10,"Swami")
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
b1 = Button(master, text='Exit', state=DISABLED, command=master.quit)
b1.grid(row=3, column=0, sticky=W, pady=4)
Button(master, text='Display', command=show_entry_fields).grid(row=3, column=1, sticky=W, pady=4)
mainloop( )
The output of the program is shown below:
This was a basic introduction of the Entry widget. With this i'll end today's post, till we meet again keep practicing and learning Python as Python is easy to learn!
The syntax of an entry widget looks like this:
w = Entry(master, option, ... )
"master" represents the parent window, where the entry widget should be placed. Like other widgets, it's possible to further influence the rendering of the widget by using options. The comma separated list of options can be empty.
The following program creates an application with two entry fields, one for entering a last name and one for the first name:
master = Tk()
Label(master, text="First Name").grid(row=0)
Label(master, text="Last Name").grid(row=1)
e1 = Entry(master)
e2 = Entry(master)
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
mainloop( )
The output of this program is shown below:
The GUI window that Tkinter creates says tk by default. To change it use the following syntax:
The GUI window that Tkinter creates says Tk by default. Here is how to change it:
master.title('Your title'). Thus we'll change the above Entry widget name to 'Name' by adding
master.title('Name'). The output now looks like:
The above program just demonstrates the creation of an entry widget. To add entry text to the widget, we use the insert method. To replace the current text, we call delete before inserting the new text. To fetch the current entry text, we use the get method.
Now let us modify our first program and get entry from user. We will also incorporate two buttons, one to display the entered text and another to quit the application. The program is shown below:
from tkinter import *
def show_entry_fields():
print("First Name: %s\nLast Name: %s" % (e1.get(), e2.get()))
master = Tk()
master.title('Name')
Label(master, text="First Name").grid(row=0)
Label(master, text="Last Name").grid(row=1)
e1 = Entry(master)
e2 = Entry(master)
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
Button(master, text='Exit', command=master.quit).grid(row=3, column=0, sticky=W, pady=4)
Button(master, text='Display', command=show_entry_fields).grid(row=3, column=1, sticky=W, pady=4)
mainloop( )
The output of the program is shown below:
Once we enter text and click display the entered text is shown in the command window:
If the user wants to quit the application he clicks exit button. Should you wish to have a default text in the entry widget use the insert() method and to delete the content of entry object use the delete() method as shown in the example below:
from tkinter import *
def show_entry_fields():
print("First Name: %s\nLast Name: %s" % (e1.get(), e2.get()))
e1.delete(0,END)
e2.delete(0,END)
master = Tk()
master.title('Name')
Label(master, text="First Name").grid(row=0)
Label(master, text="Last Name").grid(row=1)
e1 = Entry(master)
e2 = Entry(master)
e1.insert(10,"Veevaeck")
e2.insert(10,"Swami")
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
Button(master, text='Exit', command=master.quit).grid(row=3, column=0, sticky=W, pady=4)
Button(master, text='Display', command=show_entry_fields).grid(row=3, column=1, sticky=W, pady=4)
mainloop( )
The output of the program is shown below:
The default text was cleared manually and we entered new text which is then displayed in the command window. The program is ready for new entry with empty entry fields. Sometimes we want to disable a button so it can’t be clicked. Buttons have an attribute state that allows you to disable the widget. Use state=DISABLED to disable the button and state=NORMAL to enable it. The following program implements this functionality so that in the beginning the application's Exit button is disabled but once the entry is submitted it get's enabled:
from tkinter import *
def show_entry_fields():
print("First Name: %s\nLast Name: %s\n" % (e1.get(), e2.get()))
e1.delete(0,END)
e2.delete(0,END)
b1.configure(state=NORMAL)
master = Tk()
master.title('Name')
Label(master, text="First Name").grid(row=0)
Label(master, text="Last Name").grid(row=1)
e1 = Entry(master)
e2 = Entry(master)
e1.insert(10,"Veevaeck")
e2.insert(10,"Swami")
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
b1 = Button(master, text='Exit', state=DISABLED, command=master.quit)
b1.grid(row=3, column=0, sticky=W, pady=4)
Button(master, text='Display', command=show_entry_fields).grid(row=3, column=1, sticky=W, pady=4)
mainloop( )
The output of the program is shown below:
This was a basic introduction of the Entry widget. With this i'll end today's post, till we meet again keep practicing and learning Python as Python is easy to learn!
0 comments:
Post a Comment