0

I'm trying to create a custom shell in tkinter. I want it work like the usual shell where I can see my old commands. I want all the text in the same Text-widget, but I have to disable writing for the parts of the shell that have already been run. Is there an easy way to disable writing for a tag in a Text-widget?

Code (simplified):

import tkinter
import sys

class Shell:
    def __init__(self, container):
        sys.stdout = self # print() now connects to Shell-object

        self.console = tkinter.text(container)
        self.console.grid()



    def newCommand(self):
        print('>>> ')
        def run():
            # execute code
            # not actual code
            print(eval(console))

            # prepare for new command
            self.newCommand()

        # when user is done press enter
        self.console.bind('<Return>', run)

    def write(self, text):
        # old text becomes static
        self.console.tag_add('STATIC', 1.0, END)

        # print text
        self.console.insert(END, text)


tk = tkinter.Tk()

Shell(tk)


Theoul
  • 65
  • 9
  • What's your code? – jizhihaoSAMA Apr 07 '20 at 11:28
  • it's there now though it's very simplified – Theoul Apr 07 '20 at 11:49
  • I did something similar using an 'input' mark to keep track of the start of the start of the editable part and keyboard bindings to compare the position of the 'insert' and the 'input' marks. You can find a simplified version of my code in [this answer](https://stackoverflow.com/questions/59164314/how-can-i-create-a-small-idle-like-python-shell-in-tkinter/59290540#59290540). You can probably do something similar with tags, but if you want a console like behavior you will need multiple bindings (on cutting, pasting, arrow keys, backspace ...) – j_4321 Apr 07 '20 at 12:35

0 Answers0