How can I insert a string in a Entry widget that is in the "readonly" state?
Asked Answered
C

3

11

I am trying to obtain an Entry that starts with an ellipsis ....

Here was the code I tried:

e = Entry(rootWin, width=60, state="readonly")
e.insert(0, "...")

I think the error is occurring because I am trying to insert text after the object has been classified as readonly.

How can I insert a string in a Tkinter Entry widget that is in the "readonly" state?

Cracker answered 13/2, 2013 at 5:51 Comment(2)
Possible duplicate stackoverflow.com/questions/3842155Evasion
I'm needing to use the Entry object, not the Text object, since text object has no readonly state.Cracker
W
15

This seems to work for me:

import Tkinter as tk

r = tk.Tk()

e = tk.Entry(r,width=60)
e.insert(0,'...')
e.configure(state='readonly')
e.grid(row=0,column=0)

r.mainloop()
Washwoman answered 13/2, 2013 at 5:58 Comment(0)
F
15

Use -textvariable option of the Entry:

eText = StringVar()
e = Entry(rootWin, width=60, state="readonly",textvariable=eText)
....
eText.set("...I'm not inserted, I've just appeared out of nothing.")
Fash answered 13/2, 2013 at 5:55 Comment(1)
Worked for me in Python 3.12Wash
W
15

This seems to work for me:

import Tkinter as tk

r = tk.Tk()

e = tk.Entry(r,width=60)
e.insert(0,'...')
e.configure(state='readonly')
e.grid(row=0,column=0)

r.mainloop()
Washwoman answered 13/2, 2013 at 5:58 Comment(0)
D
8

The solution is simple: temporarily set the state to normal, insert the text, then set it to disabled.

Dorthydortmund answered 13/2, 2013 at 12:3 Comment(1)
Ugly, but true. I tried setting the text while in state disabled, or readonly. Neither are possible. It has to be in state normal. Anton's answer eliminates this hassle of switching back and forth, but creates another in the sense that you need a proxy of sorts.Polemics

© 2022 - 2024 — McMap. All rights reserved.