Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable input to a Text widget but allow programatic input?

How would i go about locking a Text widget so that the user can only select and copy text out of it, but i would still be able to insert text into the Text from a function or similar?

like image 823
IT Ninja Avatar asked May 30 '12 14:05

IT Ninja


People also ask

How do you disable a textbox in Python?

To disable the Entry widget, use state='disabled' property in the constructor. Disabling the Entry widget will not allow users to edit and add values to it.

How do I disable a text box?

We can easily disable input box(textbox,textarea) using disable attribute to “disabled”. $('elementname'). attr('disabled','disabled'); To enable disabled element we need to remove “disabled” attribute from this element.

Which widget is used in tkinter single line input?

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.


3 Answers

I stumbled upon the state="normal"/state="disabled" solution as well, however then you are unable to select and copy text out of it. Finally I found the solution below from: 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")
like image 61
Jonathan Avatar answered Sep 20 '22 02:09

Jonathan


Have you tried simply disabling the text widget?

text_widget.configure(state="disabled")

On some platforms, you also need to add a binding on <1> to give the focus to the widget, otherwise the highlighting for copy doesn't appear:

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

If you disable the widget, to insert programatically you simply need to

  1. Change the state of the widget to NORMAL
  2. Insert the text, and then
  3. Change the state back to DISABLED

As long as you don't call update in the middle of that then there's no way for the user to be able to enter anything interactively.

like image 26
Bryan Oakley Avatar answered Oct 08 '22 15:10

Bryan Oakley


Sorry I'm late to the party but I found this page looking for the same solution as you.

I found that if you "disable" the Text widget by default and then "normal" it at the beginning of a function that gives it input and "disable" 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")
like image 7
Andrew Avatar answered Oct 08 '22 15:10

Andrew