1

I have a Python GUI written with tkinter which calls a second function upon a button press. This function then waits for an input, using raw_input() and then acts upon the result. i.e. the function is of the format:

DO THINGS
a = raw_input('Enter value: ')
if int(a) == 0:
    do this
elif int(a) == 1:
    do something else

From my GUI, I want to send the value (0 or 1 or more options), such that the function continues to execute as I ask. I will do this with a button press in tkinter, but that's not important, the key is how to send this value to the command line. I have looked at subprocess.Popen and subprocess.communicate:

How to make python script press 'enter' when prompted on Shell

and Pexpect:

python pexpect sendcontrol key characters

However, I'm a relative novice with Python and Linux, and can't get these to work - perhaps they are correct but I just need a bit more of a specific answer for my work. I feel like this question should be very easy to solve, but I just can't find how to do it. I'd also rather work with built-in modules, so if it can be done without Pexpect that would be great - I think I'm right in saying this doesn't come as part of the initial Python installation.

Community
  • 1
  • 1
volcEmpire
  • 11
  • 2
  • 1
    Do you actually need the value your program sends to appear in the terminal? If not, it's probably better to keep it within Python. In that case you would replace `raw_input` with something else that checks for a value being sent from the button or from the terminal. – David Z May 03 '16 at 11:40
  • Can you modify the code for the function that calls `raw_input()`? If so, it's possible to set up a pipe that a subprocess will output to, that your python script can read from. – Aaron D May 03 '16 at 11:58
  • No I don't need the value to appear - I did wonder if there were better ways to communicate with the function - if this is possible and you have an example that would be great! – volcEmpire May 03 '16 at 12:03
  • Ah I've just remembered/rediscovered that if I don't call raw_input() in my function, the GUI is frozen, as it waits for the return of the function - the way I got around this was by calling raw_input(), which seems to free up my GUI again - it seems this question has become more complicated than I first thought, and probably requires a different method for me calling the function in my GUI in the first place. Any suggestions would be helpful. – volcEmpire May 04 '16 at 11:05

0 Answers0