5

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
rectangletangle
  • 50,393
  • 94
  • 205
  • 275

1 Answers1

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)

Mark
  • 106,305
  • 20
  • 172
  • 230
  • 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". – Christoph Thiede Mar 27 '23 at 18:20
  • 1
    @ChristophThiede, see new answer 13 years later. Dang, I'm old. – Mark Mar 27 '23 at 19:57
  • 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. – Christoph Thiede Mar 28 '23 at 10:53
  • 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! – Christoph Thiede Mar 31 '23 at 21:49