Make Tkinter Entry widget readonly but selectable
Asked Answered
E

1

6

Is there any way to make the Tkinter Entry widget so that text can be highlighted and copied, but not changed?

Eutherian answered 9/9, 2010 at 20:26 Comment(0)
F
8

Use the state option "disabled" (all lowercase):

Use this option to disable the Entry widget so that the user can't type anything into it. Use state=tk.DISABLED to disable the widget, state=tk.NORMAL to allow user input again. Your program can also find out whether the cursor is currently over the widget by interrogating this option; it will have the value tk.ACTIVE when the mouse is over it. You can also set this option to 'disabled', which is like the tk.DISABLED state, but the contents of the widget can still be selected or copied.


Old answer from 2010...

Use the state option "readonly":

state= The entry state: NORMAL, DISABLED, or “readonly” (same as DISABLED, but contents can still be selected and copied). Default is NORMAL. Note that if you set this to DISABLED or “readonly”, calls to insert and delete are ignored. (state/State)

Fernandes answered 9/9, 2010 at 20:33 Comment(5)
The link is no longer valid, and it seems that the "readonly" option has been removed. Under Python 3.9, I am getting the error "bad state "readonly": must be disabled or normal".Beater
@ChristophThiede, see new answer 13 years later. Dang, I'm old.Fernandes
Oh, that's very helpful, thank you! I would not have believed that tk.DISABLED and disabled would have a different behavior before trying it out. That's really not a nice design. Thanks! Edit: However, keyboard event handling seems to be limited in this mode. Ctrl + C for copy only works at the first time.Beater
EDIT 2: "Copy works only once" seems to be a general problem, independently of disabled. This might be by the design of Tkinter ... sorry for the confusion!Beater
On Ubuntu 23.04 with Python 3.11.4 'disabled' behaves just like tk.DISABLED, as far as I can tell, you won't be able to select the content.Yellowthroat

© 2022 - 2024 — McMap. All rights reserved.