Fatal text in tkinter

here is the code:

from Tkinter import * class Main(object): def __init__(self): self.console = Text(root, relief='groove', cursor='arrow', spacing1=3) self.console.insert(INSERT, '>>> ') self.console.focus_set() self.scroll = Scrollbar(root, cursor='arrow', command=self.console.yview) self.console.configure(yscrollcommand=self.scroll.set) self.scroll.pack(fill='y', side='right') self.console.pack(expand=True, fill='both') root = Tk() root.geometry('%sx%s+%s+%s' %(660, 400, 40, 40)) root.option_add('*font', ('Courier', 9, 'bold')) root.resizable(0, 1) app = Main() root.mainloop() 

Is there any way to make "โ†’>" unrecoverable (for example, in IDLE)? thanks in advance.

+4
source share
3 answers

Take a look at the IDLE source code. In particular, look at "smart_backspace_event" in EditorWindow.py. IDLE binds <Key-Backspace> to the text widget to this function (indirectly through the <<smart-backspace>> event).

The main code you will need is the following:

 chars = console.get("insert linestart", "insert") # [Do some analysis on "chars" to detect >>> and prevent a backspace] if DO_BACKSPACE: console.delete("insert-1c", "insert") # "break" is important so that the Text widget backspace handler doesn't get called return "break" 
+3
source

There is no built-in way to do this. You will need to create a set of bindings that override the default behavior, and this is not very simple. This is possible, however, since you have full control over all the bindings (i.e. no behavior is hardcoded in widgets where it cannot be changed)

Another solution, which is more bulletproof, is to intercept the low level tkinter I / O commands and check some conditions. For example, see the answer to the question fooobar.com/questions/1375953 / .... This answer provides a general solution that can be used as a hint (as asked in this question), or to mark any sections of the text as read-only.

+2
source

The "โ†’>" displayed in IDLE is part of the output of the Python interpreter. I think you can try to listen to the <Key> events and restore the prompt if necessary (see http://docs.python.org/library/tkinter.html#bindings-and-events and http://effbot.org/tkinterbook /tkinter-events-and-bindings.htm )

+1
source

All Articles