Entry widgets with Python



Entry widgets are the basic widgets of Tkinter used to get input, i.e. text strings, from the user of an application. This widget allows the user to enter a single line of text. If the user enters a string, which is longer than the available display space of the widget, the content will be scrolled. This means that the string cannot be seen in its entirety. The arrow keys can be used to move to the invisible parts of the string. If you want to enter multiple lines of text, you have to use the text widget. An entry widget is also limited to single font.

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 simple example creates an application with two entry fields. One for entering a last name and one for the first name. We use Entry without options.




The window created by the above script looks like this: 



Okay, we have created Entry fields, so that the user of our program can put in some data. But how can our program access this data? How do we read the content of an Entry?

To put it in a nutshell: The get() method is what we are looking for. We extend our little script by two buttons "Quit" and "Show". We bind the function show_entry_fields(), which is using the get() method on the Entry objects, to the Show button. So, every time this button is clicked, the content of the Entry fields will be printed on the terminal from which we had called the script.



The complete application looks now like this:



Now,
Let's assume now that we want to start the Entry fields with default values, e.g. we fill in "Miller" or "Baker" as a last name, and "Jack" or "Jill" as a first name. The new version of our Python program gets the following two lines, which can be appended after the Entry definitions, i.e. "e2 = Entry(master)":
e1.insert(10,"Miller")
e2.insert(10,"Jill")

What about deleting the input of an Entry object, every time, we are showing the content in our function show_entry_fields()? No problem! We can use the delete method. The delete() method has the format delete(first, last=None). If only one number is given, it deletes the character at index. If two are given, the range from "first" to "last" will be deleted. Use delete(0, END) to delete all text in the widget.



The output will be shown like this :



The next example shows, how we can elegantly create lots of Entry field in a more Pythonic way. We use a Python list to hold the Entry descriptions, which we include as labels into the application .



If you start this Python script, it will look like this:



Now, You can fill all these entry to show on the output screen of your IDE , or you can simply exit this window by clicking on Quit button.



Codec Networks provides the Best Python Training in delhi. Here we have an environment exactly like the actual one where they will be taught how to perform information gathering, scanning, getting access i.e. hacking, maintaining access, clearing tracks as well as how to secure their own networks. We have intensive lab environment where the student will gain practical knowledge with reference to the current security attacks and threats scenarios well-built simulated lab where the students can perform the practical under the supervision of experienced trainers who are working in the cybersecurity domains. The whole concept is to provide practical knowledge along with concept clearing in Cyber Security which is useful from career perspective in the organisation as well as for the security enthusiasts, entrepreneur. At the end of training students will have a good understanding and hands on experience in IT Security.







Article Credit to "Rohit Sharma"

Comments