1

I want to use Tk's text widget to display part of the state of my program. (A label widget will not do, because the tag feature of the text widget will save a lot of work. A canvas widget will not do, because I don't want to have to lay out a lot of text manually.)

I do not want the user to be able to directly modify the contents of the text widget. They can change the state of the program by interacting with it in other ways, but the text widget is for display only.

If I set the state of the text widget to disabled, then not only is the user unable to interact with it, but I also cannot modify its contents programmatically (specifically, I cannot insert text).

The obvious workaround is decorate any code that updates the contents of the text widget with code the enables and disables the widget. But this is kludgy: I should be able to modify the contents without offering the user an opportunity to interfere, however brief that opportunity may be.

Is there a way to do this?

MadEmperorYuri
  • 298
  • 3
  • 11
  • Show your widget code – Jeroen Heier Jan 23 '17 at 05:04
  • 1
    You are aware that this opportunity to interfere is a few hundred microseconds long, right? – TigerhawkT3 Jan 23 '17 at 05:15
  • That's exactly what you must do. Tigerhawk is correct, I've never had an issue, not even a flicker. https://github.com/GRAYgoose124/BID/blob/master/app.py#L136 – GRAYgoose124 Jan 23 '17 at 05:21
  • 1
    you have to enable, disable widget. it takes less then user thinks about pressing any button. – furas Jan 23 '17 at 07:37
  • Well asked question, well answered. Based on the TK interface, the tactic of quickly swapping cards between an enable and disable state set, feels like the logical and portable way to do it. –  Jan 06 '20 at 15:19

2 Answers2

0

The "kludgy workaround" isn't kludgy at all -- that's exactly how you do it, and how it was designed to work.

However, there is another solution. You can remove all of the built-in key and mouse bindings from the widget, and then re-implement only the ones you care about (for example, you might want the user to highlight and copy a block of text). This is simple and effective, but if you want to restore some of the bindings it starts to become very tedious to re-implement the bindings you care about (cut, copy, paste, page up, page down, moving the cursor, etc).

To remove all of the default bindings you can remove the class bind tag like this:

t.bindtags((t, root, "all"))

For more information about bind tags, see these answers:

Community
  • 1
  • 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
0

you could constantly delete then insert the text like so:

import tkinter
root = tkinter.Tk()

text = tkinter.Text(root, width=40)
text.grid(row=0, column=0, stciky=tkinter.W)

while True:
    text.delete(0.0, tkinter.END)
    text.insert(tkinter.END, your_text)

where your_text is the text you want to insert