How to send an input to a running program in the command prompt in python

Viewed 188

(i'm italian, sorry for grammar errors)

I have made a simple programming language. In this language, to run an external file, you have to type 'RUN("file_name.extension")'. Then i started making a simple text editor in tkinter able to highlights my keywords. Since in my language there is no way to run a file without running the language itself, i made that when i press the 'run' button in the editor, it opens a new cmd and run the command 'python run.py' to start the language. The run.py file expects the user input. I would like that after running the run.py file, the editor give to it the input 'RUN("the current file that is opened in the editor name") and also press automatically enter to start the program.

I have tried with subprocess, sys.stdout.write but they all give me a bunch of errors, and i couldn't find out how to simulate the press of enter. I'm sorry if somebody already asked this, but i couldn't find any answer online.

That is the editor code (i still have to add the possibility to save, open etc):

#Imports
import subprocess
from tkinter import *
import sys

#Tkinter window setup
win = Tk()
win.title("Sudo Editor")
win.geometry("1200x800")
win.resizable(False, False)

#Keywords and colors
words = {
    'IF':'purple',
    'ELIF':'purple',
    'ELSE':'purple',
    'THEN':'purple',
    'FOR':'green',
    'TO':'green',
    'STEP':'green',
    'DO':'green',
    'WHILE':'green',
    'VAR':'orange',
    'FUN':'blue',
    'RETURN':'blue',
    'BREAK':'magenta',
    'CONTINUE':'magenta',
    'END':'red',
    'NOT':'red',
    'AND':'red',
    'OR':'red',
    'PRINT':'RoyalBlue2',
    'EXIT':'RoyalBlue2',
    'CLEAR':'RoyalBlue2',
    'QUIT':'RoyalBlue2',
    'CLS':'RoyalBlue2',
    'LEN':'RoyalBlue2',
    'PRINT_RET':'RoyalBlue2',
    'INPUT':'RoyalBlue2',
    'INPUT_INT':'RoyalBlue2',
    'IS_NUM':'RoyalBlue2',
    'IS_LIST':'RoyalBlue2',
    'IS_STR':'RoyalBlue2',
    'RUN':'RoyalBlue2',
    'IS_FUN':'RoyalBlue2',
    'EXTEND':'RoyalBlue2',
    'APPEND':'RoyalBlue2',
    'POP':'RoyalBlue2',
    'NULL':'orange',
    'TRUE':'orange',
    'FALSE':'orange',
    'MATH_PI':'orange',
    '0':'red',
    '1':'red',
    '2':'red',
    '3':'red',
    '4':'red',
    '5':'red',
    '6':'red',
    '7':'red',
    '8':'red',
    '9':'red',
}

#Function colorizer
def colorize(event):
    for k,v in words.items(): # iterate over dict
        startIndex = '1.0'
        while True:
            startIndex = editor.search(k, startIndex, END) # search for occurence of k
            if startIndex:
                endIndex = editor.index('%s+%dc' % (startIndex, (len(k)))) # find end of k
                editor.tag_add(k, startIndex, endIndex) # add tag to k
                editor.tag_config(k, foreground=v)      # and color it with v
                startIndex = endIndex # reset startIndex to continue searching
            else:
                break

#Run Function
def run():
    subprocess.Popen("python run.py") #it works
    sys.stdout.write('RUN("test.py")') #it does not work (i tried also with time.sleep in the middle)
    #somehow press enter

#Widgets (not finished)
menu_bar = Menu(win)

run_bar = Menu(menu_bar, tearoff=0)
run_bar.add_command(label="Run", command=run)
menu_bar.add_cascade(label="Run", menu=run_bar)

win.config(menu=menu_bar)

editor = Text()
editor.pack(fill=BOTH)

editor.bind('<Key>', colorize)


#Main loop
win.mainloop()

and this is the run.py file (it's usless to add the language itself)

import sudo, os

while True:
    text = input('$sudo > ')
    if text.strip() == "": continue
    result, error = sudo.run('<stdin>',text)
    

    if error: 
        print(error.as_string())
    elif result: 
        if len(result.elements) == 1:
            print(repr(result.elements[0]))
        else: print(repr(result))

all helps are appreciated. Otherwise i'll just run the cmd and tell the user to write RUN() by himself, but it wouldn't be user friendly.

this is just an experiment, nothing serious

0 Answers
Related