Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set default text for a Tkinter Entry widget

How do I set the default text for a Tkinter Entry widget in the constructor? I checked the documentation, but I do not see a something like a "string=" option to set in the constructor?

There is a similar answer out there for using tables and lists, but this is for a simple Entry widget.

like image 689
Big Al Avatar asked Nov 21 '13 16:11

Big Al


People also ask

What is the difference between text and entry in Tkinter?

To enter multiple lines of text, use the Text widget. Entry in the Tkinter reference starts with: The purpose of an Entry widget is to let the user see and modify a single line of text. If you want to display multiple lines of text that can be edited, see Section 24, “The Text widget”.

What is the Tkinter default font?

Output. Running the above code will set the default font as "lucida 20 bold italic" for all the widgets that uses textual information.

Which widget we use for entry data in Tkinter?

The Entry widget is used to accept single-line text strings from a user. If you want to display multiple lines of text that can be edited, then you should use the Text widget. If you want to display one or more lines of text that cannot be modified by the user, then you should use the Label widget.


2 Answers

Use Entry.insert. For example:

try:
    from tkinter import *  # Python 3.x
except Import Error:
    from Tkinter import *  # Python 2.x

root = Tk()
e = Entry(root)
e.insert(END, 'default text')
e.pack()
root.mainloop()

Or use textvariable option:

try:
    from tkinter import *  # Python 3.x
except Import Error:
    from Tkinter import *  # Python 2.x

root = Tk()
v = StringVar(root, value='default text')
e = Entry(root, textvariable=v)
e.pack()
root.mainloop()
like image 171
falsetru Avatar answered Oct 03 '22 17:10

falsetru


For me,

Entry.insert(END, 'your text')

didn't worked.

I used Entry.insert(-1, 'your text').

Thanks.

like image 28
Nelson Hélaine Avatar answered Oct 03 '22 18:10

Nelson Hélaine