How to suppress end user ability to edit/add/delete text in a Text widget? (Python v3.2.. and tkinter)

The point is to suppress only the ability to change/add/delete text but not to castrate other features. Perhaps a NoEdit Text widged would be a better name.

I've tried .text = 'disabled' and it works almost OK in Windows (it still allows user to select/copy text highlights the selection, page up/down and up/down buttons work. The only thing broken seems to be the cursor made invisible.)

But on MacIntosh everything is broken. No highlights, no select/copy,... UGH

Since Tkinter has practically no documentation in Python, I've searched and found some TCL advise, to derive a new class and suppress the insert and delete functions.

So, I've tried as so:

class roText(tk.Text):
    def insert(self,*args,**kwargs):
        print(" Hey  - Im inside roText.insert")
        pass
    def delete(self,*args,**twargs):
        pass    
    def pInsert(self,*args,**twargs):
        super().insert(*args,**twargs)

Unfortunately it didn't work right. Apparently tkinter does not use those insert and delete functions when end user enters/deletes code. Perhaps those TCL insert/delete are something else, and I lost something in translation from TCL and Swahili. What functions does tkinter.Text use for end user editing text? Hopefully they are not internal...

So, is there a way to modify the Text widget to suppress only end user editing? Is there a way to do it without diving inside and overriding internal Tkinter code, so the stuff doesn't get broken by next releases of Tkinter?

Looking at the Idle shell window, I see that they've managed to suppress edits (except for the last line). So there is a way. But what is it and how costly?

Recommended Answers

All 6 Replies

Why don't you just use a label widget?

Because the Label widget does not provide features which I need (although Text also disables some of them with "disabled", which is the issue here).

User will interact with program by selecting a line or a portion of the text. I think Label does not allow user to select and see, nor the program to see what user selects.

And later I'll need to format the text, columns, font, color. Need a scrollbar with functional page up/down keys, etc...

Pmw has a nice set of extensions to Tkinter. I would take a look at ScrolledText for starters, as it has a get method, as do TextDialog and other similar widgets (I think). Pmw also has curselection and getcurselection methods, in the combobox and notebook, but I have not used them and am not sure if they exist in other widgets.

Re: Pmw

Thanks, but I don't think I want to experiment with a new GUI library every time there is a problem. from what I read, all of them are buggy and with major problems. Many still do not work with v3.x Python, and behave different on different platforms or don't even support them.

If I can't solve the basic problems with Python+Tkinter, I'd rather move to another more mature language, than to add bloat and more untested Python software.

Because the Label widget does not provide features which I need (although Text also disables some of them with "disabled", which is the issue here).

User will interact with program by selecting a line or a portion of the text. I think Label does not allow user to select and see, nor the program to see what user selects.

And later I'll need to format the text, columns, font, color. Need a scrollbar with functional page up/down keys, etc...

What exactly do you mean select and see, how is the user selecting? I need more context.

What exactly do you mean select and see, how is the user selecting? I need more context.

Two cases now:

1. User double-clicks on a word.
In the program I catch this with a bind and get the line.column (Like self.txt.bind('<Double-1>', self.lineSelection) )
The binding works on MacIntosh too, but the word doesn't get highlighted, so it's confusing to the user. I guess I could highlight it trough my code? But it's not my idea of platform-independent coding as Python-Tkinter is "sold" :-( I can understand platform depended differences in some graphic display, but not such a basic functionality. Scares me of what's next, since I just only started with Tkinter...

2. The simplest copy/paste does not wok on MacIntosh. User may want to coupy arbitrary part of text from my Text windows, and paste them to other applications, like a notepad or whatever. This is a basic copy/paste which one expects now to work. And again, it works on Windows, but not on a Mac (when .text['state'] = 'disabled' )

Eventually, I'll want also to "see" in the program what text user has selected. Text widget seems to provide such a functionality. But again, if it disables it for read-only window on some platforms, then it's useless.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.