Read only text widget in python3-tkinter; cross platform

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

The fact is that it only suppresses the ability to change / add / delete text , but not to castrate other functions. Perhaps a better name would be NoEdit Text widged.

I tried .text ['state'] = 'disabled' and it works almost normally on Windows (it still allows the user to select / copy text, selects the highlight, up / down pages and up / down buttons. The only thing that is broken, seems to be an invisible cursor.)

But on MacIntosh, everything is broken. No backlight, no selection / copy, ... UGH

Since Tkinter has virtually no Python documentation, I searched and found some TCL tips to get a new class and suppress insert and delete functions .

So, I tried like this:

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, this did not work. Apparently, tkinter does not use these functions insert and remove, when the end user adds / removes the code. Perhaps these TCL insertions / deletions are something else, and I lost something translated from TCL and Swahili. What features does tkinter.Text use to edit text for the end user? Hope they are not internal ...

, "", ? Tkinter, Tkinter?

Idle, , ( ). . ?

+5
3

, disabled, , Mac, , , . Mac . disabled, <ButtonPress-1>, , , .

... , , . " , ". , . , , , , .

, insert delete: - , , . . , .

, , Tcl , insert delete . , Tkinter, tcl-, , tcl-, Python.

, , , , .

, , . , , , :

import Tkinter as tk

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.text = tk.Text(width=40, height=20)
        self.text.bind("<1>", self.set_focus)
        self.text.insert("end", "\n".join(dir(tk.Tk)))
        self.text.configure(state="disabled")
        self.text.pack(fill="both", expand=True)

    def set_focus(self, event):
        '''Explicitly set focus, so user can select and copy text'''
        self.text.focus_set()

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()
+2

, , , , . , , , . .

, , , , , .

    self.txtBox.bind("<Key>", self.empty)

.

    def empty(self, event):
        return "break"

"break", , , .

, . .

+2

@BryanOakley , , Mac. , Python Mac . , .. , , :

self.txt['state'] = 'disabled'

self.txt.focus_set()

, .

"" . ..: ( ) . Python : , ( ) . , , . Shift-rightArrow , . . , ( ), ... ..

, Windows (.. , . , , / Python Mac .

, , Mac Python. , Windows. , , .. Python, .

. Mac.

-1

All Articles