0

I am working on a text based game that I run by double clicking on my top level script namely TopLevel.py. I am looking for a way to open two terminals in this script. In the one terminal the main game will be run where damage is done and spells are used etc. In the other terminal I would like to display a list of commands that the user can type in , and I want the latter one to stay there and not close until the game is finished. I am not going to show you the whole top level script (it is too long) but this is basically what I want to achieve:

    def displayCommands(hero):
        list_of_commands = []
        #this contains all my commands that the user can type in

    def main():
        hero = Hero() #make hero instance
        enemy = Enemy() #make and enemy instance
        a_game = TopLevel(hero,enemy) #create game engine
        a_game.play() #start game

        #implement code here to open another terminal 
        #and display user commands in there

Is there a way that I can open another terminal in this script and pass the displayCommands() function as a parameter to display its contents in the second terminal? Any help will be appreciated :)

Loupi
  • 550
  • 6
  • 14
  • How is the first terminal being opened and why does it need to be a powershell console? – martineau Jan 12 '15 at 11:27
  • I ask because there's a way for one Python script to spawn another in parallel via `subprocess` which will display text output piped to it in a `tkinter` window -- see the `errorwindow` module in [this answer](http://stackoverflow.com/a/18091356/355230) for details. – martineau Jan 12 '15 at 11:37
  • I appologise I only want to open one terminal with the script. (Edited the question). I run the first script by typing `python TopLevel.py` in windows powershell. Then I want to display the contents of `list_of_commands` in the second terminal that opens from `TopLevel.py`. No specific reason for using powershell I just got used to it after reading _Learn Python The Hard Way_. – Loupi Jan 13 '15 at 21:04
  • And I want the user to keep interacting with the first terminal opened. The second one should be 'static' as to just display the commands. – Loupi Jan 13 '15 at 21:05
  • 1
    The `errorwindow` module linked to indirectly in my second comment would allow you to do precisely that -- simply by `print`ing things in the primary script. – martineau Jan 13 '15 at 21:08
  • 1
    Thanks so much @martineau. Would like to accept your answer if you had one. Otherwise this question just stays unanswered. – Loupi Jan 13 '15 at 21:20

2 Answers2

0

You should convert your second program to .exe first,the one will display user commands. Say you saved it as usercommands.exe, after then in your main script use this to open that;

import subprocess
subprocess.Popen([r"usercommands.exe"],
                 creationflags=subprocess.CREATE_NEW_CONSOLE)

It'll open another console window that runs usercommands.exe when you run your main file.

Notes;

They must be in the same directory, your main file and .exe file

usercommands.exe must show the commands in an infinite loop(while True), so it's going to display commands untill the user close it.

Edit;

Now, we have some spells and usercommands will have to use that spell variables after then show us some combinations. For doing this, we have to import your first file to usercommands script.It's going to like this;

usercommands.py

import mainfile #mainfile.py

if choose == mainfile.wizard:
    print (something)
if choose == mainfile.subzero:
    print (something)

The algorithm should be like this,you will convert usercommands.py to .exe then it will work as you want I guess, we can't now before you try it ;)

GLHF
  • 3,835
  • 10
  • 38
  • 83
0

It's possible for one Python script to spawn another that will run in parallel with it via subprocess. The spawned process can then display any text piped to it (via normal print statements or calls) in a simple tkinter-based window -- see the errorwindow module in this answer of mine for more information.

It likely doesn't matter how the original script gets started. I personally have used it both in ones that were started from a command shell as well as from other tkinter based applications -- so starting yours from powershell should be fine. The module's original author was using Linux or something similar, I believe.

Community
  • 1
  • 1
martineau
  • 119,623
  • 25
  • 170
  • 301