How to disable input to a text widget but allow programmatic input?

How can I lock the Text widget so that the user can select and copy text from it, but I could still paste text into Text from a function or similar?

+7
source share
3 answers

Have you tried just disabling the text widget?

 text_widget.configure(state="disabled") 

On some platforms, you also need to add a binding on <1> to highlight the focus for the widget, otherwise the highlight for the copy will not appear:

 text_widget.bind("<1>", lambda event: text_widget.focus_set()) 

If you disable the widget to insert programmatically, you just need to

  • Change the state of the widget to NORMAL
  • Paste the text and then
  • Change state to DISABLED

Until you call update in the middle, then there is no way for the user to contribute anything interactively.

+9
source

It’s a pity that I am late for the party, but I found this page in search of the same solution as you.

I found that if you "disabled" the Text widget by default, and then "normalize" it at the beginning of the function, which gives it input and "disables" it again at the end of the function.

 def __init__(): self.output_box = Text(fourth_frame, width=160, height=25, background="black", foreground="white") self.output_box.configure(state="disabled") def somefunction(): self.output_box.configure(state="normal") (some function goes here) self.output_box.configure(state="disable") 
+1
source

I stumbled upon state = "normal" / state = "disabled" solution, but then you cannot select and copy text from it. Finally, I found the solution below: Is there a way to make the Tkinter text widget read-only? , and this solution allows you to select and copy text, as well as follow hyperlinks.

 import Tkinter root = Tkinter.Tk() readonly = Tkinter.Text(root) readonly.bind("<Key>", lambda e: "break") 
0
source

All Articles