Python Tkinter Entry Widget: A single line text input box

Saturday, May 16, 2009

I was doing GUI programming in python for sometime now and its interesting, especially to learn creating different GUI widgets, exploring the numerous options, properties and methods of those widgets and so on, Tkinter seems to be an easy interface to work with, yes there are many GUI frameworks available for python like PyGTK, wxPython, etc but I wanted to work with the one bundled as part of python distribution to start with and hence Tkinter.

It was fun and I was thrilled to see the ease in which I can create a GUI applications with as few lines of code as possible, which I want to share and keep an online reference, to start with lets explore a simple Entry widget, which is a single line text box, I came to know about Tkinter Entry widget after working with Tkinter Text widget which can be used to input multiple lines of text, but I wanted to restrict the number of lines in the text box to one, after hitting the walls (writing event handlers to restrict the number of lines in the Text widget to one), I discovered the easiest way of doing it, an Entry widget which will do the job for you, so lets start with the simple one.

This program uses multiple widgets, buts lets focus our attention only to the Entry widget part, here we go.

Listing 1: tkinterEntryWidget.py

1.  # File name: tkinterEntryWidget.py
2. # Author: S.Prasanna
3.
4. from Tkinter import *
5. import tkMessageBox
6.
7. def displayText():
8. """ Display the Entry text value. """
9.
10. global entryWidget
11.
12. if entryWidget.get().strip() == "":
13. tkMessageBox.showerror("Tkinter Entry Widget", "Enter a text value")
14. else:
15. tkMessageBox.showinfo("Tkinter Entry Widget", "Text value =" + entryWidget.get().strip())
16.
17. if __name__ == "__main__":
18.
19. root = Tk()
20.
21. root.title("Tkinter Entry Widget")
22. root["padx"] = 40
23. root["pady"] = 20
24.
25. # Create a text frame to hold the text Label and the Entry widget
26. textFrame = Frame(root)
27.
28. #Create a Label in textFrame
29. entryLabel = Label(textFrame)
30. entryLabel["text"] = "Enter the text:"
31. entryLabel.pack(side=LEFT)
32.
33. # Create an Entry Widget in textFrame
34. entryWidget = Entry(textFrame)
35. entryWidget["width"] = 50
36. entryWidget.pack(side=LEFT)
37.
38. textFrame.pack()
39.
40. button = Button(root, text="Submit", command=displayText)
41. button.pack()
42.
43. root.mainloop()

Sample Output:

Figure 1: Tkinter Entry Widget

Explanation:

For now we will focus only on the Entry widget part of the code, lines 33 - 36 creates an Entry widget inside a textFrame (line 26) and the Submit button (line 40), when clicked calls the displayText method (line 7) which uses the Entry widget's get method to fetch the text value of Entry widget (line 12), an error message is printed if no text is input (line 13) and the value of the text otherwise (line 15), in the next blog, we will improve the above code by restricting the text input size.

6 comments:

wuy3 said...

nice find, thanks for writing this one up.

yybar said...

Dear Prisanna,

Thankyou for the post. I am presently attempting to write my own GUI and am running into a problem when I attempt to invoke the get() attribute for the Entry() method. Looking at your example, it seems that is should work. I have enclosed a copy of my code and ask for your help. Many Thanks, Robert

import Tkinter
from Tkinter import *

def Test():
global widgetEntry
strVal = widgetEntry.get()
print str(strVal)


root = Tk() textFrame = Frame(root)

widgetLabel = Label(textFrame)
widgetLabel.config(text = 'Enter text')
widgetLabel.pack(side = LEFT)
widgetLabel.pack(expand = YES, fill = BOTH)

widgetEntry = Entry(textFrame).pack(side = LEFT)
textFrame.pack()
Button(root, text = 'Submit', command=Test).pack(side=LEFT)

root.mainloop()

Anonymous said...

Thanks, very clear and useful.

Gianfro

Anonymous said...

The pack() function returns None...

Anonymous said...

Thanks prsasanna

Mike said...

Is there a way to return the input with instead of a button?


Copyright © 2016 Prasanna Seshadri, www.prasannatech.net, All Rights Reserved.
No part of the content or this site may be reproduced without prior written permission of the author.